jQuery的:抓阿贾克斯例外 [英] jQuery: catch ajax exceptions
问题描述
许多失败的jQuery的Ajax请求有污染我有错误控制台。综观其产生这些控制台错误code(jQuery的1.7.2,行8240)
//不要发送请求
//这可能会引发异常这实际上是
//在jQuery.ajax(这里所以没有的try / catch)来处理
xhr.send((s.hasContent&安培;&安培; s.data)|| NULL);
我注意到评论解释了为什么没有尝试
/ 抓
有。但是,即使我有我的 jQuery.ajax
要求明确的错误
的回调函数,我的还是的不具有由 jQuery.ajax
处理这些错误。
我该如何处理这样的方式的错误消息做jQuery的阿贾克斯错误的没有的出现在控制台?
编辑:的下面是我的code段,做Ajax请求,和precise错误信息(在Chrome):
$。阿贾克斯({
数据类型:XML,
网址:./python/perfdata.xml?sid=+(新的日期()),
成功:功能(数据){
VAR协议= $(协议,数据)。儿童();
parseData(协议);
},
错误:功能(错误){
的setTimeout(的getData,200);
}
});
这里是Chrome浏览器的错误信息:
GET
http://zeus/dashboard/python/perfdata.xml?sid=Thu%20May%2024%202012%2016:09:38%20GMT+0100%20(GMT%20Daylight%20Time)
的jquery.js:8240
您可以试试这个(镀铬效果很好),如果进行测试,通过重写功能发送:
$(函数(){
VAR XHR = NULL;
如果(window.XMLHtt prequest){
XHR = window.XMLHtt prequest;
}
否则如果(window.ActiveXObject('Microsoft.XMLHTTP')){
//我不知道这是否正常工作
XHR = window.ActiveXObject(Microsoft.XMLHTTP);
}
VAR发送= xhr.prototype.send;
xhr.prototype.send =功能(数据){
尝试{
// TODO:评论下一行
的console.log('pre发送,数据);
send.call(此,数据);
// TODO:评论下一行
的console.log(POS发送');
}
赶上(E){
// TODO:评论下一行
的console.log(ERR发送,E);
}
};
$阿贾克斯({
数据类型:XML,
网址:./python/perfdata.xml?sid=+(新的日期())的getTime()。
成功:功能(数据){
VAR协议= $(协议,数据)。儿童();
parseData(协议);
},
错误:功能(错误){
的setTimeout(的getData,200);
}
});
});
Many failed jQuery ajax requests are polluting my console with errors. Looking at the code which produces these console errors (jQuery 1.7.2, line 8240)
// Do send the request
// This may raise an exception which is actually
// handled in jQuery.ajax (so no try/catch here)
xhr.send( ( s.hasContent && s.data ) || null );
I notice the comment explains why there is no try
/catch
there. However, even though I have an explicit error
callback function in my jQuery.ajax
request, I'm still not having those errors handled by jQuery.ajax
.
How can I handle jQuery ajax errors in such a way that error messages do not appear in the console?
Edit: Below is my code snippet that does the ajax request, and the precise error message (in Chrome):
$.ajax({
dataType: 'xml',
url: "./python/perfdata.xml?sid=" + (new Date()),
success: function (data) {
var protocols = $("Protocols", data).children();
parseData(protocols);
},
error: function (error) {
setTimeout(getData, 200);
}
});
And here is the Chrome error message:
GET
http://zeus/dashboard/python/perfdata.xml?sid=Thu%20May%2024%202012%2016:09:38%20GMT+0100%20(GMT%20Daylight%20Time)
jquery.js:8240
You can try this (in chrome works well) if for testing, by overriding the function "send":
$(function() {
var xhr = null;
if (window.XMLHttpRequest) {
xhr = window.XMLHttpRequest;
}
else if(window.ActiveXObject('Microsoft.XMLHTTP')){
// I do not know if this works
xhr = window.ActiveXObject('Microsoft.XMLHTTP');
}
var send = xhr.prototype.send;
xhr.prototype.send = function(data) {
try{
//TODO: comment the next line
console.log('pre send', data);
send.call(this, data);
//TODO: comment the next line
console.log('pos send');
}
catch(e) {
//TODO: comment the next line
console.log('err send', e);
}
};
$.ajax({
dataType: 'xml',
url: "./python/perfdata.xml?sid=" + (new Date()).getTime(),
success: function (data) {
var protocols = $("Protocols", data).children();
parseData(protocols);
},
error: function (error) {
setTimeout(getData, 200);
}
});
});
test
这篇关于jQuery的:抓阿贾克斯例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!