Javascript 对于 AJAX 调用,可以用 xhr.onload 替换 xhr.onreadystatechange 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14946291/
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
Can one replace xhr.onreadystatechange with xhr.onload for AJAX calls?
提问by
I need to support major modern browsers only (IE10+, FF, Chrome, Safari)
我只需要支持主要的现代浏览器(IE10+、FF、Chrome、Safari)
Can I make this substitution as I want to simplify my code base:
我可以做这个替换,因为我想简化我的代码库:
From:
从:
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
o.callback(xhr.responseText);
} else {
return false;
}
} else {
return false;
}
};
To:
到:
xhr.onload = function (test) {
o.callback(xhr.responseText);
};
I don't feel that the MDN documentationis clear in this regard.
我不认为MDN 文档在这方面很清楚。
Clarification:
澄清:
I choose not to use a framework.
我选择不使用框架。
采纳答案by mr.VVoo
maybe you take a look at this oneand a look at W3C: XMLHttpRequestit's the same if your browser supports xhr.onload. Requires XMLHttpRequest 2)
也许你看看这个,看看W3C: XMLHttpRequest如果你的浏览器支持 xhr.onload 也是一样的。需要 XMLHttpRequest 2)
You can also write a wrapping function that emulates xhr.onload if it's not present.
(I think you need to override XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges somehow}). If you only support modern browsers using xhr.onload should be available - the best solution is using a framework(like jqueryor mootoolsthat provides wrapping functionality for that.
如果 xhr.onload 不存在,您还可以编写一个模拟 xhr.onload 的包装函数。(我认为你需要覆盖XMLHttpRequest.prototype.onload = function(args){//calling onreadystatechanges somehow})。如果您只支持使用 xhr.onload 的现代浏览器应该可用 - 最好的解决方案是使用框架(如jquery或mootools为其提供包装功能。
回答by Gert Cuykens
In the MDN documentation they state the following
在 MDN 文档中,他们声明了以下内容
Events
onreadystatechange as a property on the xhr object is supported in all browsers.
Since then, a number of additional event handlers were implemented in various browsers (onload, onerror, onprogress, etc.). These are supported in Firefox. In particular, see nsIXMLHttpRequestEventTarget and Using XMLHttpRequest.
More recent browsers, including Firefox, also support listening to the XMLHttpRequest events via standard addEventListener APIs in addition to setting on* properties to a handler function.
活动
所有浏览器都支持 onreadystatechange 作为 xhr 对象的属性。
从那时起,在各种浏览器中实现了许多额外的事件处理程序(onload、onerror、onprogress 等)。这些在 Firefox 中受支持。特别是,请参阅 nsIXMLHttpRequestEventTarget 和使用 XMLHttpRequest。
除了将 on* 属性设置为处理函数之外,包括 Firefox 在内的最新浏览器还支持通过标准 addEventListener API 侦听 XMLHttpRequest 事件。
So I think you can assume that onreadystatechange is the way to go and onload is a addition that can be used if the browser supports it. @mr.VVoo answer is the correct one, to stick to w3c when in doubt.
所以我认为你可以假设 onreadystatechange 是要走的路,而 onload 是一个可以在浏览器支持的情况下使用的附加功能。@mr.VVoo 答案是正确的,有疑问时坚持使用 w3c。
回答by íhor Mé
Since the original asker said "I need to support major modern browsers only (IE10+, FF, Chrome, Safari)" it is obvious that onload, onerror, etc are the ways to go. Also, as of today, onreadystatechangeis pretty much obsolete, as you are obviously not going to support anything that old that it supports onreadystatechangeonly.
由于原来的提问者说:“我需要支持主要的现代浏览器只(IE10 +,FF,Chrome浏览器,Safari浏览器)”很明显onload,onerror等都是走的方式。此外,截至今天,onreadystatechange它几乎已经过时了,因为您显然不会支持onreadystatechange它只支持的任何旧版本。
To sum it up, forget about onreadystatechange.
总结一下,忘记onreadystatechange。
回答by user126440
You can clean up your first example by doing this
你可以通过这样做来清理你的第一个例子
xhr.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
o.callback(xhr.responseText);
} else {
return false;
}
};
Note, you probably want to check onloadtoo with this.status === 200if you are doing something with the else statement. Although, if you are checking for errors, there is also onerror, which you can write something like
注意,你可能要检查onload过用this.status === 200,如果你正在做的事情与else语句。虽然,如果您正在检查错误,还有onerror,您可以编写类似
xhr.onerror = function(){
console.log('Error: Do something else...');
}

