Javascript 处理AJAX错误、未登录等-对这种方法有何想法?
我正在尝试使用jQuery设置一个通用的AJAX成功/错误处理程序,我可以在多个项目上使用它。我这样做的理由是,在AJAX请求过程中可能会发生许多“错误”。以下是我到目前为止处理过的问题和状态编号:Javascript 处理AJAX错误、未登录等-对这种方法有何想法?,javascript,ajax,jquery,Javascript,Ajax,Jquery,我正在尝试使用jQuery设置一个通用的AJAX成功/错误处理程序,我可以在多个项目上使用它。我这样做的理由是,在AJAX请求过程中可能会发生许多“错误”。以下是我到目前为止处理过的问题和状态编号: 0:未知错误 1:成功 2:未登录 3:超时 4:不允许(基于权限) 404错误 对于前5个错误,我必须手动处理它们。要做到这一点,我能找到的唯一方法是在ajax调用的success函数中运行cl4.process\u ajax方法,使用JSON数据中的status键/属性来确定状态是什么。对于
- 0:未知错误
- 1:成功
- 2:未登录
- 3:超时
- 4:不允许(基于权限)
- 404错误
cl4.process\u ajax
方法,使用JSON数据中的status键/属性来确定状态是什么。对于404或其他一些AJAX错误(即不可解析的JSON),我在jQuery中使用了ajaxError()
global事件处理程序
基本思想是,当出现错误时,错误将显示在屏幕顶部#ajax_errors
内,向下滑动使其看起来“漂亮”。可以单击每个错误以隐藏它们
这是我如何使用它的一个例子cl4.process\u ajax
也可以放在if子句中,只允许代码在成功时发生
$.getJSON('/path/to/page?c_ajax=1', function(return_data) {
cl4.process_ajax(return_data);
if (return_data !== null && typeof return_data == 'object' && typeof return_data.html != 'undefined') {
$('#div').html(return_data.html);
} else {
$('#div').html('');
}
});
查询参数c_ajax
告诉登录和权限检查返回JSON数组,而不是像通常那样将用户重定向到登录或无访问页面
只需包含error\u msg key/property,即可在服务器上轻松自定义显示的消息。这将使处理诸如翻译之类的事情变得容易,并且使代码更加灵活。我已经包括了在调试时显示调试消息的功能,因此在开发过程中很容易确定错误是什么(相对于一般用户消息)
JSON编码的数组应类似于:
array(
status => the status using the constants in AJAX
error_msg => message to be displayed the user at the top of the page
debug_msg => message to be displayed in debug mode
html => the html to display
... any other data for that request
)
以下是错误处理代码:
// setup the cl4 object and the debug flag
// these are typically else where and cl4 has other stuff in it
var cl4 = {};
var cl4_in_debug = TRUE; // probably default to false, but for this makes it easier to test
cl4.default_error_msg = 'There was a error loading some of the content on this page.<br>Try reloading the page or contacting an administrator.';
/**
* attach an AJAX error hander to the ajax_error element
*/
$('#ajax_errors').ajaxError(function() {
cl4.add_ajax_error(cl4.default_error_msg);
});
/**
* Call within a ajax success function to deal with the response of an ajax call
*/
cl4.process_ajax = function(return_data) {
if (typeof return_data != 'object' || return_data === null) {
cl4.add_default_ajax_error(return_data);
return false;
}
if (cl4.in_debug && typeof return_data.debug_msg != 'undefined'/* && return_data.debug_msg != ''*/) {
cl4.add_ajax_error(return_data.debug_msg);
}
// check to see if we've received the status, because we need it for the rest
if (typeof return_data.status == 'undefined') {
return;
}
switch (return_data.status) {
// successful
case 1 :
return true;
break;
// not logged in
case 2 :
cl4.add_default_ajax_error(return_data, 'You are no longer logged in. <a href="/login">Click here to login.</a>');
return false;
break;
// timed out
case 3 :
cl4.add_default_ajax_error(return_data, 'Your login has timed out. To continue using your current login, <a href="/login/timedout">click here to enter your password.</a>');
return false;
break;
// not allowed (permissions)
case 4 :
cl4.add_default_ajax_error(return_data, 'You do not have the necessary permissions to access some of the functionality on this page.');
return false;
break;
// unknown error
case 0 :
default :
cl4.add_default_ajax_error(return_data);
return false;
}
};
/**
* ajax error function, will show a red div at the top of the page if there is a problem with any of the ajax on the page
*/
cl4.add_ajax_error = function(error) {
$('#ajax_errors').append('<div>' + error + '</div>');
$('#ajax_errors div').click(function() {
$(this).slideUp(function() {
$(this).remove();
});
}).slideDown();
};
/**
* Adds a default message if there is no error_msg in the return_data object
*/
cl4.add_default_ajax_error = function(return_data, default_msg) {
if (arguments.length == 1) {
default_msg = cl4.default_error_msg;
}
if (return_data !== null && typeof return_data == 'object' && typeof return_data.error_msg != 'undefined' && return_data.error_msg != '') {
cl4.add_ajax_error(return_data.error_msg);
} else {
cl4.add_ajax_error(default_msg);
}
};
//设置cl4对象和调试标志
//这些通常是其他地方,cl4还有其他东西
var cl4={};
var cl4_in_debug=TRUE;//可能默认为false,但这使得测试更容易
cl4.default_error_msg='加载此页面上的某些内容时出错。
请尝试重新加载页面或与管理员联系';
/**
*将AJAX错误处理程序附加到AJAX\u错误元素
*/
$('#ajax_errors').ajaxError(函数(){
cl4.add_ajax_error(cl4.default_error_msg);
});
/**
*在ajax成功函数中调用,以处理ajax调用的响应
*/
cl4.process\u ajax=函数(返回\u数据){
if(返回数据的类型!='object'| |返回数据===null){
cl4.add\u default\u ajax\u error(返回\u数据);
返回false;
}
if(cl4.in_debug&&typeof return_data.debug_msg!='undefined'/*&&return_data.debug_msg!='*/){
cl4.add\u ajax\u error(返回\u data.debug\u msg);
}
//检查我们是否已收到状态,因为其余部分都需要它
如果(返回的类型\u data.status==“未定义”){
返回;
}
开关(返回数据状态){
//成功的
案例1:
返回true;
打破
//未登录
案例2:
添加默认错误(返回数据“您不再登录”);
返回false;
打破
//超时
案例3:
添加\u默认\u ajax\u错误(返回\u数据,“您的登录已超时。若要继续使用当前登录,”);
返回false;
打破
//不允许(权限)
案例4:
cl4.add_default_ajax_error(返回_data,'您没有访问此页面上某些功能所需的权限');
返回false;
打破
//未知错误
案例0:
违约:
cl4.add\u default\u ajax\u error(返回\u数据);
返回false;
}
};
/**
*ajax错误函数,如果页面上的任何ajax出现问题,将在页面顶部显示一个红色div
*/
cl4.add\u ajax\u error=函数(错误){
$('#ajax_errors')。追加(''+error+'');
$('#ajax_errors div')。单击(函数(){
$(this.slideUp(function()){
$(this.remove();
});
}).slideDown();
};
/**
*如果返回数据对象中没有错误消息,则添加默认消息
*/
cl4.add\u default\u ajax\u error=函数(返回数据,默认消息){
if(arguments.length==1){
default\u msg=cl4.default\u error\u msg;
}
if(返回数据!==null&&typeof返回数据=='object'&&typeof返回数据.error\u msg!='undefined'&&return\u数据.error\u msg!=''){
cl4.add\u ajax\u error(返回\u data.error\u msg);
}否则{
cl4.add_ajax_error(默认消息);
}
};
有什么想法吗?有更好的方法吗?除了无法解析的数据或404(或类似)错误之外,我在处理AJAX“错误”方面没有发现太多。另外,在jQuery中有没有更好的方法来处理这些类型的错误。(我尝试使用成功处理程序,但它们似乎都是在当前请求的成功处理程序之后运行的。)
对不起,代码太多了…我希望它能短一点,但是
顺便说一句,我想将其设置为community wiki,但出于某种原因,我没有复选框。如果您的检查函数返回一个布尔值,指定处理程序是否应继续执行,那么您就可以执行类似的操作,这将更加简洁:
$.getJSON('/path/to/page?c_ajax=1', function(return_data) {
if(!cl4.process_ajax(return_data)) return;
$('#div').html(return_data.html);
});
当然,您也可以在if块中清除div,但我认为这个示例很好地说明了我的意图(如果可以在错误检查器中完成,则不在处理程序本身中编写与基本错误检查相关的代码)。我在这种情况下执行此操作的原因,尽管在很多情况下我会像您所说的那样:(1)我把一个加载图标放在