2010-05-17 78 views
0

要從$ .ajax調用中返回一個錯誤,有一個比在ajax.php文件中回顯錯誤然後修剪它更好的方法!jquery ajax sucess - 可能返回JSON而不是字符串?

這似乎很笨拙和穩健:

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!');} 
} 

我應該怎麼來代替幹什麼?

回答

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); 
    } 
} 
+0

啊涼,這樣我就可以回聲出/ json_encode在一些腳本標籤上ajax.php文件? – Haroldo 2010-05-17 16:46:13

相關問題