views:

165

answers:

1

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.

+1  A: 

For security reasons, this is not possible.

SLaks
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
You could use your own server as a proxy and use ordinary AJAX.
SLaks
@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