AJAX 返回异常状态代码
所以我终于开始再次尝试 AJAX(而不是隐藏的 iframe),现在我记起了为什么我一开始那么讨厌 AJAX。
这是一个简单的脚本,用于向服务器查询用户收到的私人消息的数量。服务器以单个数字响应,脚本更新指示器并设置五秒的超时以再次查询。
在极少数情况下(通常是当我离开一段时间时),我开始收到最奇怪的 HTTP 状态代码。
在 Chrome 上:“AJAX 请求无法检索 /ajax/network.php,HTTP 状态 0”
在 IE 上:“AJAX 请求无法检索 /ajax/network.php,HTTP 状态 12029”
这些代码均无效。这是怎么回事?我怎样才能阻止它发生?
So I've finally gotten around to trying AJAX again (rather than hidden iframes), and I now remember why I hated AJAX so much in the first place.
It's a simple script that queries the server for the number of private messages the user has received. The server responds with a single number, and the script updates the indicator and sets a timeout for five seconds to query again.
On rare occasion (usually when I go AFK for a while), I start getting the most bizarre HTTP status codes.
On Chrome: "AJAX request failed to retrieve /ajax/network.php, HTTP status 0"
On IE: "AJAX request failed to retrieve /ajax/network.php, HTTP status 12029"
Neither of these codes are valid. What's going on here, and how can I stop it from happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 AJAX 的一个令人烦恼的方面。我发现最好将所有非 200 状态简单地视为错误。实际上,您不会向用户显示与实际结果相关的任何特定错误消息(“发生了问题”),因此我不会过度担心不一致的错误条件。
jQuery 标准化了一些 AJAX 行为,因此您可能还想考虑使用它。
顺便说一句,Chrome中的错误0和IE中的错误12029表示连接问题;因为这与 HTTP 无关,所以会显示无效代码。
That is an annoying aspect of AJAX. I find it's best to simply treat all non-200 statuses as an error. Really, you're not going to show your users any specific error messages relating to the actual result ("A problem has occurred"), so I'd simply not worry overly about inconsistent error conditions.
jQuery normalizes some of the AJAX behavior, so you may also want to look into using that.
By the way, error 0 in Chrome and 12029 in IE indicate connection problems; since that has nothing to do with HTTP, that's why invalid codes are shown.