最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - How to calculate Content-Length in JavaScript - Stack Overflow

programmeradmin1浏览0评论
function LikesDislikes () {

    $.ajax({
        url: '',
        type:"POST",
        data: '<?xml version="1.0" encoding="UTF-8"?>
            <entry xmlns=""
                   xmlns:yt="">
                <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?

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?

Share Improve this question edited Jul 20, 2013 at 14:41 Anoop 23.2k10 gold badges64 silver badges79 bronze badges asked Jul 20, 2013 at 13:31 user2595123user2595123 1411 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 9

As said in specs

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs

Please look as this question String length in bytes in JavaScript.

Basically if your data contains only ASCII characters everything should be quite easy

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);
        }
    });
}

The Content-Length entity header is indicating the size of the entity-body, in bytes, sent to the recipient.

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.

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

发布评论

评论列表(0)

  1. 暂无评论