需要捕获 ajax 跨域脚本加载错误
Need to catch ajax crossdomain script load error
过去一个月我一直在处理这个问题,我几乎阅读了 Whosebug 上的所有 post,但没办法......我最好的方法是:
$.ajax({
url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
xhr: function () {
var xhr = new window.XMLHttpRequest();
//Not working...
xhr.onerror = function(e) {
alert("Error Status: " + e.target.status);
};
//Frustration ends here...
return xhr;
}
})
.done(function(data) {
console.log("Ajax success");
init();
callback();
});
"connect1" 使请求失败,但由于著名的跨域请求问题,我无法找到捕获错误的方法。我最好的选择是实现我自己的 xhr 对象,但没有触发 onerror 事件。
我写这篇文章 post 因为任何新的或新鲜的想法都会对我有很大帮助。
更新: 这是可用于复制请求的 url,旨在失败:https://connect1.facebook.net/es_LA/sdk.js?_=1445285186108
提前致谢。
吉列尔莫
要检查不同于脚本错误的响应代码,请将 statusCode 回调添加到您的 ajax。
$.ajax({
url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
statusCode: {
404: function() {
alert( "page not found" );
}
},
xhr: function () {
var xhr = new window.XMLHttpRequest();
//Not working...
xhr.onerror = function(e) {
alert("Error Status: " + e.target.status);
};
//Frustration ends here...
return xhr;
}
})
.done(function(data) {
console.log("Ajax success");
init();
callback();
});
另外,既然URL好像是JS,你不反对使用JQuery,为什么不使用jQuery.getScript()呢?
$.getScript( "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js");
最后感谢@Wobbles 帮了我很多,我们在这里找到了答案:
解决方案包括向 ajax 请求添加超时。
超时过后,将触发 onerror 事件,您可以从那里用您的代码响应错误。
$.ajax({
url: "https://connect.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
timeout: 5000, // Wait 5 secs to get the sucess
error: function(jqXHR, textStatus, errorThrown) {
if(textStatus === "timeout") {
//We waited 5 secs and the request hasnt succeed.
//Code to manage the error.
}
}
})
此致,
吉列尔莫
过去一个月我一直在处理这个问题,我几乎阅读了 Whosebug 上的所有 post,但没办法......我最好的方法是:
$.ajax({
url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
xhr: function () {
var xhr = new window.XMLHttpRequest();
//Not working...
xhr.onerror = function(e) {
alert("Error Status: " + e.target.status);
};
//Frustration ends here...
return xhr;
}
})
.done(function(data) {
console.log("Ajax success");
init();
callback();
});
"connect1" 使请求失败,但由于著名的跨域请求问题,我无法找到捕获错误的方法。我最好的选择是实现我自己的 xhr 对象,但没有触发 onerror 事件。
我写这篇文章 post 因为任何新的或新鲜的想法都会对我有很大帮助。
更新: 这是可用于复制请求的 url,旨在失败:https://connect1.facebook.net/es_LA/sdk.js?_=1445285186108
提前致谢。
吉列尔莫
要检查不同于脚本错误的响应代码,请将 statusCode 回调添加到您的 ajax。
$.ajax({
url: "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
statusCode: {
404: function() {
alert( "page not found" );
}
},
xhr: function () {
var xhr = new window.XMLHttpRequest();
//Not working...
xhr.onerror = function(e) {
alert("Error Status: " + e.target.status);
};
//Frustration ends here...
return xhr;
}
})
.done(function(data) {
console.log("Ajax success");
init();
callback();
});
另外,既然URL好像是JS,你不反对使用JQuery,为什么不使用jQuery.getScript()呢?
$.getScript( "https://connect1.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js");
最后感谢@Wobbles 帮了我很多,我们在这里找到了答案:
解决方案包括向 ajax 请求添加超时。
超时过后,将触发 onerror 事件,您可以从那里用您的代码响应错误。
$.ajax({
url: "https://connect.facebook.net/" + ('BR' == $('#country').val() ? 'pt_BR' : 'es_LA') + "/sdk.js",
dataType: "script",
cache: false,
timeout: 5000, // Wait 5 secs to get the sucess
error: function(jqXHR, textStatus, errorThrown) {
if(textStatus === "timeout") {
//We waited 5 secs and the request hasnt succeed.
//Code to manage the error.
}
}
})
此致,
吉列尔莫