ajax的readystate为3,为什么在做ajax时无法获得readyState 3(why can't get readyState 3 when doing a ajax)...

为什么在做ajax时无法获得readyState 3(why can't get readyState 3 when doing a ajax)

我从书中读到,如果我从服务器执行ajax请求,我将从0 - 4获得readyState。这是我的代码:

xhr = new XMLHttpRequest();

console.log(xhr.readyState);

xhr.onreadystatechange = function() {

console.log(xhr.readyState);

};

xhr.open('GET', 'http://localhost:81/data.txt', true);

xhr.send();

我从控制台看到0 1 2 4退出,而3不是,为什么会发生这种情况? 请帮我谢谢〜

i read from books that if i do a ajax request from sever, i will get readyState from 0 - 4. here is my code:

xhr = new XMLHttpRequest();

console.log(xhr.readyState);

xhr.onreadystatechange = function() {

console.log(xhr.readyState);

};

xhr.open('GET', 'http://localhost:81/data.txt', true);

xhr.send();

i see from the console that 0 1 2 4 exit, and 3 is not, why does this happen? pls help me thank you~

原文:https://stackoverflow.com/questions/31203743

更新时间:2019-10-15 22:04

最满意答案

你有一个小的有效载荷,所以RS3过快,响应是NULL

AND / OR

您的回复在Chrome中返回NULL(假设您使用的是Chrome),因为这是Webkit浏览器对RS3的处理方式

尝试设置......

Content-type: text/xml

此外,你需要在Chrome中的RS3中发送除NULL之外的任何响应之前至少推送1k数据,尽管“官方”阈值是256b,它不正确并且Chrome直到大约1k +已经渲染才开始发送。

所有webkit浏览器基本上都是这样

但是没关系,因为你不需要RS3的任何东西。 RS4是你唯一真正需要担心的事情。

You have a small payload so RS3 goes by so fast the response is NULL

AND/OR

Your response is returning NULL in Chrome (Assuming you are using Chrome) because that is what webkit browsers do with RS3

Try setting ...

Content-type: text/xml

Also you need to push at least 1k of data before it sends any response besides NULL in RS3 in Chrome, though the "official" threshold is 256b, its not correct and Chrome doesn't start sending until about 1k+ has been rendered.

This is basically true of all webkit browsers

But it's OK because you don't need anything from RS3. RS4 is the only thing you really need to worry about.

相关问答

如果浏览器实现正确,快速回答是YES。 见这里: http : //www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#current-document-readiness quick answer is YES if browser implementation is correct. See here: http://www.whatwg.org/specs/web-apps/current-work/multipage/

...

request不是全局request ,因此您无法从interpretRequest函数访问它。 改用this 。 request isn't a global, so you can't access it from the interpretRequest function. Use this instead.

你为什么重新发明轮子? 如果您正在使用mootools,那么发出ajax请求非常容易( docs ,指的是最新版本,但在这种情况下请求没有更改): new Request({

url : './gdwcommreply_genxml.php?getfield=idvideo&fieldid=64&parentid=59',

onSuccess : function(responseText, responseXML){

/* here, do stuff with

...

你有一个小的有效载荷,所以RS3过快,响应是NULL AND / OR 您的回复在Chrome中返回NULL(假设您使用的是Chrome),因为这是Webkit浏览器对RS3的处理方式 尝试设置...... Content-type: text/xml

此外,你需要在Chrome中的RS3中发送除NULL之外的任何响应之前至少推送1k数据,尽管“官方”阈值是256b,它不正确并且Chrome直到大约1k +已经渲染才开始发送。 所有webkit浏览器基本上都是这样 但是没关系,因为你不需要RS3

...

在这里找到答案: Ajax请求在真实设备上的cordova / phonegap应用程序失败 最后一个答案适用于我

到config.xml,仍然想知道为什么

不适用。 Found the answer here: Ajax request failing in cordova

...

在webkits开发者论坛上做了大量研究和阅读之后,我终于可以告诉我们发生了什么以及如何在chrome / webkit / safari预防这种情况。 这都是关于连接类型的。 为了使chrome在“ ready state 3 “正确”工作(responseText为非null),我们需要一个不受嗅探的内容类型。 例如,Content-type:text / xml工作正常。 webkit另一个重要事项是,在传输足够的字节之前,它不会呈现推送的数据。 “官方”该treshhold约为256字节。

...

我执行了这个: function getRealtime() {

var dataxxx;

$.ajax({

type: 'GET',

url: 'cpuload.php',

data: {

'get_server_load': 1

},

dataType: 'json',

async: false,

success: function(data)

...

您错误地分配了处理函数: xhr.onreadystatechange = handler; //

当您包括括号时,您要求调用该函数。 没有它们,你只是指功能,这就是你想要的。 You're assigning the handler function incorrectly: xhr.onreadystatechange = handler; //

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值