jQuery 如何在 JavaScript 中计算 Content-Length
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17762778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate Content-Length in JavaScript
提问by user2595123
function LikesDislikes () {
$.ajax({
url: 'http://gdata.youtube.com/feeds/api/videos/keDZXXDxK1c/ratings',
type:"POST",
data: '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<yt:rating value="like"/>
</entry>',
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);
}
});
}
How can I calculate the Content-Length
in the above code?
如何计算Content-Length
上面代码中的?
回答by twil
As said in specs
正如规格中所说
The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs
Content-Length entity-header 字段指示实体主体的大小,以十进制的 OCTET 数表示
Please look as this question String length in bytes in JavaScript.
请看这个问题String length in bytes in JavaScript。
Basically if your data contains only ASCII characters everything should be quite easy
基本上,如果您的数据只包含 ASCII 字符,一切都应该很容易
function LikesDislikes () {
var data = '<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<yt:rating value="like"/>
</entry>';
$.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);
}
});
}
回答by Gil Epshtain
The Content-Lengthentity header is indicating the size of the entity-body, in bytes, sent to the recipient.
在Content-Length的实体报头指示该实体主体的大小,以字节为单位,发送到接收方。
Syntax
句法
Content-Length: <length>
Directives
指令
<length>
The length in decimal number of octets.
Content-Length Calculation
内容长度计算
- If the request body is a String you can simple use the length of the body.
- If the request body is a JSON you can just stringify the body.
- 如果请求正文是字符串,您可以简单地使用正文的长度。
- 如果请求正文是 JSON,您可以将正文字符串化。
const requestBody =
{
data:
{
...
}
};
xhr.setRequestHeader("Content-Length", JSON.stringify(requestBody).length.toString());
Read more at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
阅读更多信息:https: //developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length