我使用的是jQuery的getJSONP,我希望记录调用的持续时间和响应的大小,以便能够获得有关应用程序使用情况的一些统计信息。这是一个跨域ajax调用,所以我需要使用JSONP,但是由于JSONP调用不是使用XMLHttpRequest对象完成的,所以来自jquery的ajax的完整回调不会传递响应内容。
因此,我的问题是如何从JSONP调用中获取响应大小(内容长度)。
$.ajaxSetup(
{
complete:function(x,e)
{
log(x.responseText.length, x.responseText);
}
}
这里的x是一个JSON调用的XMLHttpRequest对象,但是for JSONP调用是未定义的。
发布于 2013-03-11 20:35:00
您可以获取响应头部的Content-Length:
var contentsize;
$.ajax('url', function(data, textstatus, request) {
contentsize = request.getResponseHeader("Content-Length") / 1024;
//do stuff with your data
});
发布于 2016-05-16 22:07:50
$.ajax('url',function(data,textstatus,request)
{
var totalBytes = request.getResponseHeader('Content-length');
//if u are looking for downloaded bytes
var dlBytes =request.responseText.length;
});
https://stackoverflow.com/questions/2645504
复制相似问题