gpt4 book ai didi

javascript - XMLHttpRequest 的 onerror 处理程序应该什么时候触发

转载 作者:IT王子 更新时间:2023-10-29 03:06:59 29 4
gpt4 key购买 nike

我在理解 XMLHttpRequest 的处理程序时遇到一点问题。 The specification says this about the onerror handler :

error [Dispatched ... ] When the request has failed.

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

问题是,“请求失败”是什么意思。这可能是

  • 请求根本无法发出(例如,连接被拒绝等错误),或者
  • 以上再加上服务器返回了一个错误码(例如404)

此外,我想知道这是否意味着 onerroronload 不应同时触发。

This reference指示 onerror 处理程序应根据 status 代码和 onload 取决于 readyState 执行。这表明它们不是相互排斥的,但是,我认为这不是权威信息。

我问是因为使用最新的 Opera 快照,我发现 onload 即使在 404 状态代码上也会被触发。我知道测试 status 是肯定的,但我想知道这是我必须按照规范做的事情,还是只是 Opera 中错误的解决方法。

最佳答案

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

请注意,被拒绝的跨域请求也会触发 onerror 处理程序。

关于javascript - XMLHttpRequest 的 onerror 处理程序应该什么时候触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10584318/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com