http://www.w3.org/TR/access-control/
阅读上面链接的CORS规范,似乎不可能可靠地区分一般的“网络错误”和跨源访问拒绝错误。从规范中:
如果存在网络错误,请应用网络错误步骤。
执行资源共享检查。如果返回失败,请应用网络错误步骤。
http://www.w3.org/TR/access-control/#simple-cross-origin-request0
在我的测试中,我无法找到Firefox实现的任何功能,这些特性似乎表明资源共享检查肯定失败了。它只是将readyState切换到4,并将状态设置为0。
最终,我希望能够将一个成功的回调、一个普通的失败回调和一个可选的跨源失败回调传递给我的函数。谢谢你的帮助或见解。
发布于 2010-05-18 23:31:33
您可能不需要发出XHR请求就可以知道何时会发生跨源错误。在jQuery的$.get基础上尝试以下内容
var xhr = {
get: function(url, data, callback, dataType) {
if ( !this.isSameOrigin(url) ) {
callback(null, "Same-origin error or whatever", null);
}
$.get(url, data, callback, dataType);
},
isSameOrigin: function(url) {
// Do a string comparison against window.location. Get as complicated
// as you'd like
return !!(
// Url doesn't contain a valid protocol (relative to domain))
!url.match(/^https?:\/\//i) ||
// Url contains a protocol but the request is to the current domain
url.match(new RegExp("^https?://" + window.location.host, "i"))
);
}
};https://stackoverflow.com/questions/2861271
复制相似问题