提供无效url时,jquery get()不会给出错误
我注意到,当我向提供无效url时,jquery get()不会给出错误,jquery,Jquery,我注意到,当我向get()提供一个无效的URL时,我没有收到任何错误 $.get(nextHref, function(data, status, xhr) { alert(status); if (status == "error") { alert("an error has occured: " + xhr.status + " " + xhr.statusText); } 警报(状态)nv甚至在我从页面提供无效URL时运行: 如果带有jQuery.get()的请求返
get()
提供一个无效的URL时,我没有收到任何错误
$.get(nextHref, function(data, status, xhr) {
alert(status);
if (status == "error") {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
警报(状态)代码>nv甚至在我从页面提供无效URL时运行:
如果带有jQuery.get()的请求返回错误代码,则除非脚本还调用了global.ajaxError()方法,否则它将以静默方式失败
阅读有关ajaxError的信息
基本上,您需要做的是将ajaxError
附加到某个项目,并从那里处理错误:
我们可以将事件处理程序附加到任何元素:
$('.log').ajaxError(function() {
$(this).text('Triggered ajaxError handler.');
});
$('.trigger').click(function() {
$('.result').load('ajax/missing.html');
});
现在,我们可以使用任何jQuery方法发出Ajax请求:
$('.log').ajaxError(function() {
$(this).text('Triggered ajaxError handler.');
});
$('.trigger').click(function() {
$('.result').load('ajax/missing.html');
});
从页面:
如果带有jQuery.get()的请求返回错误代码,则除非脚本还调用了global.ajaxError()方法,否则它将以静默方式失败
阅读有关ajaxError的信息
基本上,您需要做的是将ajaxError
附加到某个项目,并从那里处理错误:
我们可以将事件处理程序附加到任何元素:
$('.log').ajaxError(function() {
$(this).text('Triggered ajaxError handler.');
});
$('.trigger').click(function() {
$('.result').load('ajax/missing.html');
});
现在,我们可以使用任何jQuery方法发出Ajax请求:
$('.log').ajaxError(function() {
$(this).text('Triggered ajaxError handler.');
});
$('.trigger').click(function() {
$('.result').load('ajax/missing.html');
});
回调函数是一个success函数,因此它永远不会在出错时运行,您可以将其用于完整版本,也可以装配一个全局错误事件处理程序,以更合适的为准
如果要使用,请使用error
回调,如下所示:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
或者像这样使用全局事件处理程序:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
或者,用于为所有请求添加错误
处理程序,如下所示:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
回调函数是一个success函数,因此它永远不会在出错时运行,您可以将其用于完整版本,也可以装配一个全局错误事件处理程序,以更合适的为准
如果要使用,请使用error
回调,如下所示:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
或者像这样使用全局事件处理程序:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
或者,用于为所有请求添加错误
处理程序,如下所示:
$.ajax({
url: nextHref,
success: function(data) {
//do something with good data, what comes after your if statement currently
},
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
$(document).ajaxError(function(e, xhr) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
});
$.ajaxSetup({
error: function(xhr, status, error) {
alert("an error has occured: " + xhr.status + " " + xhr.statusText);
}
});
如果希望捕获错误,请使用以下命令:
$.ajax({
url : url,
success : function(result) {
//doSomething
},
error : function(request, status, error) {
if(status == 'parsererror' || status == 'error') {
//doSomething
}
}
});
如果希望捕获错误,请使用以下命令:
$.ajax({
url : url,
success : function(result) {
//doSomething
},
error : function(request, status, error) {
if(status == 'parsererror' || status == 'error') {
//doSomething
}
}
});