如何处理getJSON调用中的错误?我试图使用jsonp引用跨域脚本服务,你如何注册错误方法?
我的目标是检测广告拦截器.我找到了几个很好的例子,比如FuckAdBlock.
广告服务调用被广告拦截阻止后,我们会收到错误"err_blocked_by_client".
我想通过以下方式处理此错误:
var xhr = new XMLHttpRequest();
try
{
xhr.open("GET","http://static.adzerk.net/ados.js", false);
xhr.onreadystatechange = function (oEvent) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText)
} else {
console.log("Error", xhr.statusText);
}
}
};
xhr.send();
}
catch(error)
{
console.log("ConsoleLog \n " + JSON.stringify(xhr));
console.log("Error Catched" + error);
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我无法识别catch块的错误原因.
请让我知道在此代码中处理此错误或错误的更好选项.
谢谢