本文介绍了当应XMLHtt prequest的onError的处理程序火灾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题理解 XMLHtt prequest 的处理程序。 规范说这有关的onerror 处理

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

错误 [调度......当请求失败。

负荷 [调度......当请求成功完成。

load [Dispatched ... ] When the request has successfully completed.

现在的问题是,这是什么意思说,请求失败。这可能是

The problem is, what does it mean that "the request has failed". That could be

  • 的请求无法在所有发行(如:连接被拒绝和这样的错误),或
  • 在上面加了服务器返回错误code(例如404)

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

Also, I'd like to know whether it means onerror and onload should never fire simultaneously.

该参考表示的onerror 处理程序应该是根据状态 code和的onload 根据的readyState 。这将表明他们不是相互排斥的,但是,我不认为这是一个权威的信息。

This reference indicates 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.

我这么问是因为采用了最新的Opera快照,我发现的onload 甚至在404状态code解雇。我知道测试状态是一个肯定的赌注,但我想知道是否这件事情我已经按规格或只是一种解决方法做了歌剧中的错误。

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.

推荐答案

正如在评论中,的onerror 火灾时,有上的网络级故障的。如果错误只存在于应用层面上,例如,HTTP错误code发送,然后的onload 仍是起火。你需要在你的 onreadystatuschange 处理程序明确地测试返回的状态code。

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 onreadystatuschange handler.

请注意,一个拒绝跨域请求会激活的onerror 处理程序。

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

这篇关于当应XMLHtt prequest的onError的处理程序火灾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:31