jquery ajax成功 - 可能返回JSON而不是字符串?

时间:2010-05-17 16:33:25

标签: jquery ajax

要从$ .ajax调用返回错误,必须有一个比回显ajax.php文件中的错误然后修剪它更好的方法!

这看起来非常笨拙和坚强:

success: function(e){ 
    var e = trim(e);
    if(e == 'SUCCESS')
        {alert('your password has been changed!');}   
    if(e == 'ERROR1')
        {alert('please fill in all inputs!');}
    if(e == 'ERROR2')
        {alert('password incorrect!');}
    if(e == 'ERROR3')
        {alert('change failed!');} 
} 

我应该做什么呢?!

1 个答案:

答案 0 :(得分:2)

返回JSON:

{ success: false, errorMessage: 'please fill in all inputs!' }

然后:

success: function(e) {
    if(e.success) {
        alert('your password has been changed!');
    }
    else {
        alert(e.errorMessage);
    } 
}