ajax响应字节大小 [英] ajax response byte size

查看:168
本文介绍了ajax响应字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery的getJSONP,我想记录调用的持续时间和响应的大小,以便能够获得有关我的应用程序使用情况的一些统计信息。
这是一个跨域的ajax调用,所以我需要使用JSONP,但由于JSONP调用没有使用XMLHttpRequest对象,因此jquery的ajax的完整回调不会传递响应内容。

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.

所以我的问题是如何从JSONP调用获得响应大小(内容长度)。

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

这里x是JSON调用的XMLHttpRequest对象,但对于JSONP调用未定义。

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

推荐答案

您可以获得响应标题的Content-Length:

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

这篇关于ajax响应字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆