Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - ajax response byte size

I'm using jQuery's getJSONP and I want to log the duration of the call and the size of the response to be able to have some statistics about the usage of my application. This is a cross domain ajax call, so I need to use JSONP, but as the JSONP call is not done with an XMLHttpRequest object, the complete callback from jquery's ajax doesn`t pass the response content.

So my question is how to get the response size (content lenght) from a JSONP call.

$.ajaxSetup(
{
    complete:function(x,e)
    {
         log(x.responseText.length, x.responseText);
    }
}

here x is a XMLHttpRequest object for a JSON call , but for JSONP call is undefined.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can get the "Content-Length" of the response header:

var contentsize;
$.ajax('url', function(data, textstatus, request) {
    contentsize = request.getResponseHeader("Content-Length") / 1024;
    //do stuff with your data
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...