Javascript XMLHttpRequest 的 onerror 处理程序何时应该触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10584318/
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
When should XMLHttpRequest's onerror handler fire
提问by jpalecek
I have a little problem understanding XMLHttpRequest
's handlers. The specification says this about the onerror
handler:
我在理解XMLHttpRequest
的处理程序时遇到了一些问题。规范说明了有关onerror
处理程序的内容:
error
[Dispatched ... ] When the request has failed.
load
[Dispatched ... ] When the request has successfully completed.
error
[Dispatched ... ] 当请求失败时。
load
[Dispatched ... ] 当请求成功完成时。
The problem is, what does it mean that "the request has failed". That could be
问题是,“请求失败”是什么意思。那可能是
- the request couldn't be issued at all (eg. Connection refused and such errors), or
- the above plus the server returned an error code (eg. 404)
- 根本无法发出请求(例如连接被拒绝和此类错误),或
- 以上加上服务器返回错误代码(例如404)
Also, I'd like to know whether it means onerror
and onload
should never fire simultaneously.
另外,我想知道这是否意味着onerror
并且onload
不应该同时开火。
This referenceindicates the onerror
handler should be executed depending on the status
code and onload
depending on readyState
. That would indicate they are not mutually exclusive, however, I don't think this is an authoritative information.
此参考指示onerror
应根据status
代码和onload
根据readyState
. 这表明它们并不相互排斥,但是,我认为这不是权威信息。
I'm asking because using the latest Opera snapshot, I found onload
is fired even on 404 status code. I know testing status
is a sure bet, but I'd like to know whether it's something I have to do per specification or just a workaround for a bug in Opera.
我问是因为使用最新的 Opera 快照,我发现onload
即使在 404 状态代码上也会被触发。我知道测试status
是一个肯定的赌注,但我想知道这是我必须按照规范做的事情还是只是 Opera 中错误的解决方法。
回答by apsillers
As mentioned in the comments, onerror
fires when there is a failure on the network level. If the error only exists on the application level, e.g., an HTTP error code is sent, then onload
still fires. You need to test the returned status code explicitly in your onreadystatechange
handler.
如评论中所述,onerror
当网络级别出现故障时触发。如果错误仅存在于应用程序级别,例如,发送了 HTTP 错误代码,则onload
仍会触发。您需要在onreadystatechange
处理程序中明确测试返回的状态代码。
Note that a denied cross-domain request will also fire the onerror
handler.
请注意,被拒绝的跨域请求也会触发onerror
处理程序。
回答by StanE
In addition to apsillers' answer, note that XMLHttpRequest handles redirects automatically in the background, so you don't have to check for this reply codes in the onload
event (this event will be invoked only once - on the final call). Also note, that in case you send payload data with the POST method and if the requests is redirected, XMLHttpRequest change the method from POST
to GET
and discards any payload data for security reasons. The onload
event will still be invoked but you will need to re-send your request manually again to the new destination.
除了 apsillers 的回答,请注意 XMLHttpRequest 在后台自动处理重定向,因此您不必在onload
事件中检查此回复代码(此事件只会被调用一次 - 在最终调用时)。另请注意,如果您使用 POST 方法发送有效负载数据并且请求被重定向,则 XMLHttpRequest 会将方法从 更改POST
为GET
并出于安全原因丢弃任何有效负载数据。该onload
事件仍将被调用,但您需要再次手动将您的请求重新发送到新目的地。