function LikesDislikes () { $.ajax({ url: 'http://gdata.youtube.com/feeds/api/videos/keDZXXDxK1c/ratings', type:"POST", data: ' ', headers: { "Content-Type":"application/atom+xml", "Content-Length":, "Authorization":"Bearer ya29.AHES6ZQ59RrQgujZmIjssBdYlwwLVrpCodnirdLROi7-g7U", "X-GData-Key":"key=AIzaSyAPrtP2Tq4m5WVInCvCWptVAKPhQ4SQNZA", "GData-Version":"2" }, // Content-Type:"application/atom+xml", error: function() { alert("No data found."); }, // contentType: "text/xml", success: function (response) { alert('response:' + response); } }); }
Comment puis-je calculer la Content-Length
dans le code ci-dessus?
Comme dit dans les spécifications
Le champ d’en-tête d’entité Content-Length indique la taille du corps de l’entité, en nombre décimal d’OCTET.
Veuillez regarder comme cette question Longueur de chaîne en octets en JavaScript .
Fondamentalement, si vos données ne contiennent que des caractères ASCII, tout devrait être assez facile
function LikesDislikes () { var data = ' '; $.ajax({ url: 'http://gdata.youtube.com/feeds/api/videos/keDZXXDxK1c/ratings', type:"POST", data: data, headers: { "Content-Type":"application/atom+xml", "Content-Length": data.length, "Authorization":"Bearer ya29.AHES6ZQ59RrQgujZmIjssBdYlwwLVrpCodnirdLROi7-g7U", "X-GData-Key":"key=AIzaSyAPrtP2Tq4m5WVInCvCWptVAKPhQ4SQNZA", "GData-Version":"2" }, // Content-Type:"application/atom+xml", error: function() { alert("No data found."); }, // contentType: "text/xml", success: function (response) { alert('response:' + response); } }); }
L’en – tête d’entité Content-Length indique la taille du corps de l’entité, en octets, envoyée au destinataire.
Content-Length:
The length in decimal number of octets.
const requestBody = { data: { ... } }; xhr.setRequestHeader("Content-Length", JSON.ssortingngify(requestBody).length.toSsortingng());
Plus d’informations sur: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length