3

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33
  • 1
    Being pedantic, `x.responseText.length` gives you the *character* count, not the byte count. – T.J. Crowder Apr 15 '10 at 13:23
  • For security reasons, this is not possible. – SLaks Apr 15 '10 at 13:13
  • hi SLaks, thanks for the quick answer, but then the question arises: is there a best practice for achieving this? for example I can have the response length as a direct property in the json response. but that means that if I have no control over the server side I cannot do that. – Alex Pacurar Apr 15 '10 at 13:23
  • You could use your own server as a proxy and use ordinary AJAX. – SLaks Apr 15 '10 at 13:26
  • @Alex: I think SLaks is (as usual) correct that you can't (although I quibble with his definition of *why*), there just isn't any way to do this. Basically what you're asking is for a way to get the length of an external script that is included in your page via a `script` tag (because that's how JSONP works, by temporarily inserting a `script` tag in your document). If you had control of the server side of things, then of course there are several ways you could do this, but if you don't... – T.J. Crowder Apr 15 '10 at 13:30

2 Answers2

6

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
});
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Entrabiter
  • 752
  • 7
  • 13
  • 1
    I tried this way, but for me content-length header is always returning 0 :( – Saurabh Kumar Jul 14 '14 at 15:50
  • This will not always work. It only works if the server includes the content-length header. Here's an image that you can retrieve that does not include the header: http://diarioveaonline.com/wp-content/uploads/2018/06/unique-beautiful-nature-wallpaper-for-desktop-free-download-16-5b2ff2c66582c.jpg – Johann Aug 28 '18 at 09:15
1
$.ajax('url',function(data,textstatus,request)
{
     var totalBytes  = request.getResponseHeader('Content-length');

     //if u are looking for downloaded bytes
     var dlBytes =request.responseText.length; 
});
  • While this code may answer the question, it would be better to include some _context_, explaining _how_ it works and _when_ to use it. Code-only answers are not useful in the long run. – Benjamin W. May 16 '16 at 14:24
  • This answer is correct for each case. Case 1: Response is Gzip Case 2: Response is Plain – Milind Singh Mar 29 '19 at 14:24