来自 AJAX 操作页面的错误处理
error handling from AJAX action page
我使用 PHP、AJAX 和 jQuery 开发了我的网络应用程序。一切顺利。
对应用程序的大部分请求来自 AJAX;所有插入、更新、删除和 select
我已经对应用程序中无法通过 AJAX 访问的页面进行了一些错误处理。
我的问题是;在我的应用程序中 AJAX 被使用了数千次。现在需要付出很多努力才能为所有这些端点添加错误处理。
我的问题是;有没有一种方法可以让我在 AJAX 中使用某种形式的通用错误处理,或者我可以从 AJAX 操作页面重定向到另一个显示错误的页面(因为那样也可以)?
我 99% 确定这是不可能的,但我需要一些专家建议。
看来你的代码已经复制了一千遍了,效率不高;-)
执行以下操作。
函数 CommunicateWithServer(设置){
// 做 ajax 事情
$.ajax({
type: "POST",
async: settings.async,
contentType: "application/json",
dataType: "json",
url: settings.url + settings.method,
data: settings.data,
}).done(function (d) {
// do a generic thing on done
settings.done(d);
}).fail(function(d) {
// do a generic thing on fail here
if (settings.fail) {
settings.fail(d);
}
}).always(function (d) {
settings.always(d);
});
}
并在您现在直接使用 jquery Ajax 的任何地方调用此代码。
这样您就可以在程序生命周期中实现新的通用功能。
要使用此代码:
把这个函数放在一个js文件中,每个页面都会用到,然后不用$.Ajax(),而是:
hereYourSettings = {
async:false,
url : "yourUrl",
method: "yourMethod",
data: {dataIsCool:true}, // <== Put your data object here
done: function(rawData){alert("yes!");},
fail: function(rawData){alert("snap!");},
always: function(rawData){alert("And I'ieII.., Will always love you!");},
}
与服务器通信(这里是您的设置);
当您坚持保留旧代码时:
使用:http://api.jquery.com/ajaxerror/
$( document ).ajaxError(function( event, jqxhr, settings, thrownError ) {
if ( settings.url == "ajax/missing.html" ) {
$( "div.log" ).text( "Triggered ajaxError handler." );
}
});
看看ajaxSetup in jQuery docs。使用 ajaxSetup,您可以为应用程序发出的所有 AJAX 请求设置默认值。例如:
$.ajaxSetup({
type: 'POST', // All AJAX requests will be POST unless explicitly specified in the AJAX call,
error: function() {
// Your error handling for all AJAX calls.
}
});
现在,如果你这样打电话:
$.ajax({
url: '/savedata/',
data: somedata,
})
由于 $.ajaxSetup
的默认设置,这将是一个 POST 请求。但是,如果您提出这样的请求:
$.ajax({
type: 'GET',
url: '/getdata/',
error: function(){
// Different error handling code
}
});
这将是一个 GET 请求,因为您已经用不同的错误处理方式覆盖了 $.ajaxSetup
的默认设置。
我使用 PHP、AJAX 和 jQuery 开发了我的网络应用程序。一切顺利。 对应用程序的大部分请求来自 AJAX;所有插入、更新、删除和 select
我已经对应用程序中无法通过 AJAX 访问的页面进行了一些错误处理。
我的问题是;在我的应用程序中 AJAX 被使用了数千次。现在需要付出很多努力才能为所有这些端点添加错误处理。
我的问题是;有没有一种方法可以让我在 AJAX 中使用某种形式的通用错误处理,或者我可以从 AJAX 操作页面重定向到另一个显示错误的页面(因为那样也可以)?
我 99% 确定这是不可能的,但我需要一些专家建议。
看来你的代码已经复制了一千遍了,效率不高;-)
执行以下操作。
函数 CommunicateWithServer(设置){ // 做 ajax 事情
$.ajax({
type: "POST",
async: settings.async,
contentType: "application/json",
dataType: "json",
url: settings.url + settings.method,
data: settings.data,
}).done(function (d) {
// do a generic thing on done
settings.done(d);
}).fail(function(d) {
// do a generic thing on fail here
if (settings.fail) {
settings.fail(d);
}
}).always(function (d) {
settings.always(d);
});
}
并在您现在直接使用 jquery Ajax 的任何地方调用此代码。 这样您就可以在程序生命周期中实现新的通用功能。
要使用此代码:
把这个函数放在一个js文件中,每个页面都会用到,然后不用$.Ajax(),而是:
hereYourSettings = {
async:false,
url : "yourUrl",
method: "yourMethod",
data: {dataIsCool:true}, // <== Put your data object here
done: function(rawData){alert("yes!");},
fail: function(rawData){alert("snap!");},
always: function(rawData){alert("And I'ieII.., Will always love you!");},
}
与服务器通信(这里是您的设置);
当您坚持保留旧代码时: 使用:http://api.jquery.com/ajaxerror/
$( document ).ajaxError(function( event, jqxhr, settings, thrownError ) {
if ( settings.url == "ajax/missing.html" ) {
$( "div.log" ).text( "Triggered ajaxError handler." );
}
});
看看ajaxSetup in jQuery docs。使用 ajaxSetup,您可以为应用程序发出的所有 AJAX 请求设置默认值。例如:
$.ajaxSetup({
type: 'POST', // All AJAX requests will be POST unless explicitly specified in the AJAX call,
error: function() {
// Your error handling for all AJAX calls.
}
});
现在,如果你这样打电话:
$.ajax({
url: '/savedata/',
data: somedata,
})
由于 $.ajaxSetup
的默认设置,这将是一个 POST 请求。但是,如果您提出这样的请求:
$.ajax({
type: 'GET',
url: '/getdata/',
error: function(){
// Different error handling code
}
});
这将是一个 GET 请求,因为您已经用不同的错误处理方式覆盖了 $.ajaxSetup
的默认设置。