栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery $ .ajax,错误处理程序不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery $ .ajax,错误处理程序不起作用

更新 :尝试将测试替换

(data == null)
(XMLHttpRequest.readyState === 4 &&XMLHttpRequest.status === 0)

在有关

XMLHttpRequest
标准的W3C候选推荐中,描述了它必须存在,因此命名为“错误标志”,该标志应用于指示某种类型的网络错误或中止。如果发生此类错误,状态
XMLHttpRequest
为0,而不是200(“确定”),201(“已创建”),304(“未修改”)等等。要完全对应于http://www.w3.org/TR/XMLHttpRequest/#the-
status-attribute
XMLHttpRequest.status
,如果
XMLHttpRequest.readyState
等于0或1(“未发送”或“已打开”),则可以为0
。另一种情况
XMLHttpRequest.readyState
等于4(“完成”),并且“错误标志”为true。如果我们没有这两种情况,则
XMLHttpRequest.status
必须为HTTP状态代码。在http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html下或http://www.w3.org/Protocols/HTTP/1.0/spec.html#Status-
Codes没有等于0的HTTP状态代码。所以在我看来,您可以执行以下操作

jQuery(document).ready(function () {    $.ajax({        type: 'GET',        url:  'test/GameConfiguration.json',        dataType: 'json',        cache : false,        success: function(data, textStatus, xhr){ if (xhr.readyState === 4 && xhr.status === 0) {     // if readyState is DONE (numeric value 4) then corresponds to     // http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-done     // "The DONE state has an associated error flag that indicates     // some type of network error or abortion."     // Corresponds to http://www.w3.org/TR/XMLHttpRequest/#the-status-attribute     // If error flag it true, xhr.status is 0.     alert('"error flag" is true. It means that we have a network error"+" or abortion (for example because of Same Origin Policy restrictions)'); } else {     alert(textStatus);     alert(data); }        },        error: function(xhr, textStatus, errorThrown) { if (textStatus !== null) {     alert("error: " + textStatus); } else if (errorThrown !== null) {     alert("exception: " + errorThrown.message); } else {     alert ("error"); }        }    });});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418332.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号