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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:58:41  来源:igfitidea点击:

When should XMLHttpRequest's onerror handler fire

javascriptajaxxmlhttprequest

提问by jpalecek

I have a little problem understanding XMLHttpRequest's handlers. The specification says this about the onerrorhandler:

我在理解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 onerrorand onloadshould never fire simultaneously.

另外,我想知道这是否意味着onerror并且onload不应该同时开火。

This referenceindicates the onerrorhandler should be executed depending on the statuscode and onloaddepending 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 onloadis fired even on 404 status code. I know testing statusis 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, onerrorfires 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 onloadstill fires. You need to test the returned status code explicitly in your onreadystatechangehandler.

如评论中所述,onerror网络级别出现故障时触发。如果错误仅存在于应用程序级别,例如,发送了 HTTP 错误代码,则onload仍会触发。您需要在onreadystatechange处理程序中明确测试返回的状态代码。

Note that a denied cross-domain request will also fire the onerrorhandler.

请注意,被拒绝的跨域请求也会触发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 onloadevent (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 POSTto GETand discards any payload data for security reasons. The onloadevent will still be invoked but you will need to re-send your request manually again to the new destination.

除了 apsillers 的回答,请注意 XMLHttpRequest 在后台自动处理重定向,因此您不必在onload事件中检查此回复代码(此事件只会被调用一次 - 在最终调用时)。另请注意,如果您使用 POST 方法发送有效负载数据并且请求被重定向,则 XMLHttpRequest 会将方法从 更改POSTGET并出于安全原因丢弃任何有效负载数据。该onload事件仍将被调用,但您需要再次手动将您的请求重新发送到新目的地。