开发者

jquery ajax sucess - possible to return JSON instead of string?

开发者 https://www.devze.com 2022-12-30 08:31 出处:网络
To return an error from a $.ajax call, there\'s gotta be a better way than echoing an error in the ajax.php file and then trimming it!

To return an error from a $.ajax call, there's gotta be a better way than echoing an error in the ajax.php file and then trimming it!

this seems very clumsy and in-robust:

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

what should i be doing instead?!


return JSON:

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

and then:

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

精彩评论

暂无评论...
验证码 换一张
取 消