javascript jQuery - 如何检查 AJAX 调用中响应对象的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7791860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:20:45  来源:igfitidea点击:

jQuery - How to check the size of the response object in an AJAX call?

javascriptjqueryajax

提问by Roman

I want to know the size (in kbyte / byte) of the response to an ajax call (.get, .post, .ajax) - any ideas?

我想知道对 ajax 调用(.get、.post、.ajax)的响应的大小(以千字节/字节为单位) - 有什么想法吗?

Thanks!

谢谢!

采纳答案by GolezTrol

I figure you 's best check this size in the debugger tools of your browser. You can get the data length, as suggested by John Riche, but this is only the character length. The actual byte size of this data alone may vary, depending on the used encoding. And then you got your headers too.

我认为您最好在浏览器的调试器工具中检查此大小。您可以按照 John Riche 的建议获取数据长度,但这只是字符长度。仅此数据的实际字节大小可能会有所不同,具体取决于所使用的编码。然后你也得到了你的标题。

Unfortunately I think you cannot read this information from Javascript itself.

不幸的是,我认为您无法从 Javascript 本身读取此信息。

If you use Chrome, press F12 to open the development tools, and choose 'Network', you get an overview of requests, including AJAX requests. For each request there's a byte size of both the response content and the entire response. In FireBug you should be able to see similar data.

如果您使用 Chrome,按 F12 打开开发工具,然后选择“网络”,您将获得请求概览,包括 AJAX 请求。对于每个请求,响应内容和整个响应都有一个字节大小。在 FireBug 中,您应该能够看到类似的数据。

回答by jonjbar

Just check data.length. Here is an example:

只需检查 data.length。下面是一个例子:

$.get('ajax/test.html', function(data) {
  alert(data.length);
});

回答by Entrabiter

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

你可以得到响应头的“Content-Length”:

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