JavaScript 的 try-catch 因类型错误而失败
JavaScript's try-catch is failing on typeerror
今天刚在我们的应用程序上看到这个错误。我们在网页的右上角有一个小方框。它每分钟更新一次。它所做的是通过内部网向我们的每个医院合作伙伴发出 jsonp 调用并获取最新消息。这个问题并不经常发生。我实际上发现了错误发生的原因。
Uncaught TypeError: jQuery11020382352269484832883_1441911920555 is not a function
当 jsonp 请求在 jsonp 超时指定的时间内没有从 jsonp 服务获得响应时,控制台中会出现此错误。
我用 try-catch 包装了我们的端点调用,但它仍然吐出那个错误。我想摆脱 "Uncaught TypeError" 并显示我们自己的自定义错误。
当您使用 jQuery 并且 http 请求是异步的时,您无法使用 try-catch
处理此类错误,这只会捕获请求调用的错误,而不是整个错误过程。
一些研究给了我这个 link : http://bugs.jquery.com/ticket/8744
实际上jsonpCallback 函数不应该在请求超时时调用,这似乎是一个浏览器错误:http://bugs.jquery.com/ticket/8744#comment:2 https://bugzilla.mozilla.org/show_bug.cgi?id=707154
jQuery 错误报告 (e.marin.izquierdo
) 中的某人给出了 "handle" 此错误的可能解决方案。 (我稍微修改了一下,删除了不相关的东西)
var auxTime = new Date();
var jQueryCallbackRandom = auxTime.getTime();
var callParameters = {
url: 'http://jsfiddle.net/echo/jsonp/',
timeout: 5,
dataType: "jsonp",
data: { echo: "Hello World!" },
jsonpCallback: "jQueryRandom_" + jQueryCallbackRandom,
success: function(){
console.log("success");
},
error: function(jqXHR, textStatus){
console.log("failed with error: " + textStatus);
window["jQueryRandom_" + jQueryCallbackRandom] = function() {
window["jQueryRandom_" + jQueryCallbackRandom] = null;
};
}
};
$.ajax(callParameters);
它在错误侦听器中创建 jsonpCallback 以避免错误,但为此您需要知道 jsonpCallback
.
的名称
今天刚在我们的应用程序上看到这个错误。我们在网页的右上角有一个小方框。它每分钟更新一次。它所做的是通过内部网向我们的每个医院合作伙伴发出 jsonp 调用并获取最新消息。这个问题并不经常发生。我实际上发现了错误发生的原因。
Uncaught TypeError: jQuery11020382352269484832883_1441911920555 is not a function
当 jsonp 请求在 jsonp 超时指定的时间内没有从 jsonp 服务获得响应时,控制台中会出现此错误。
我用 try-catch 包装了我们的端点调用,但它仍然吐出那个错误。我想摆脱 "Uncaught TypeError" 并显示我们自己的自定义错误。
当您使用 jQuery 并且 http 请求是异步的时,您无法使用 try-catch
处理此类错误,这只会捕获请求调用的错误,而不是整个错误过程。
一些研究给了我这个 link : http://bugs.jquery.com/ticket/8744
实际上jsonpCallback 函数不应该在请求超时时调用,这似乎是一个浏览器错误:http://bugs.jquery.com/ticket/8744#comment:2 https://bugzilla.mozilla.org/show_bug.cgi?id=707154
jQuery 错误报告 (e.marin.izquierdo
) 中的某人给出了 "handle" 此错误的可能解决方案。 (我稍微修改了一下,删除了不相关的东西)
var auxTime = new Date();
var jQueryCallbackRandom = auxTime.getTime();
var callParameters = {
url: 'http://jsfiddle.net/echo/jsonp/',
timeout: 5,
dataType: "jsonp",
data: { echo: "Hello World!" },
jsonpCallback: "jQueryRandom_" + jQueryCallbackRandom,
success: function(){
console.log("success");
},
error: function(jqXHR, textStatus){
console.log("failed with error: " + textStatus);
window["jQueryRandom_" + jQueryCallbackRandom] = function() {
window["jQueryRandom_" + jQueryCallbackRandom] = null;
};
}
};
$.ajax(callParameters);
它在错误侦听器中创建 jsonpCallback 以避免错误,但为此您需要知道 jsonpCallback
.