2012-03-27 52 views
1

我在使用AJAX和JQuery提交表單時出錯,下面是我用來提交表單的代碼。使用Jquery提交AJAX表單時出錯

function formControl() { 
    $('form#form_contact').submit(function(){ 
     if (verifyErrors()) { 
      action = $(this).attr('action'); 
      mesagetosend = $(this).serialize(); 
      $.ajax({ 
        type: "POST", 
        url: 'mailscript/smtpmail.php', 
        data: mesagetosend, 
        dataType : "html", 
        timeout:10000, 
        error: function(msg) { 
         alert("Error:="+msg); 
         onerrormsg(msg); 
        }, 
        success: function(msg){ 
         alert("Success:="+msg); 
         onsuccessmsg(msg); 
        } 
       }); 
      } 
     return false; 
    }); 
} 

在警告我越來越"Error:=[object XMLHttpRequest]"

做任何人有任何想法,爲什麼它會返回錯誤???

+1

使用'的console.log(味精);',而不是'警報( 「錯誤:=」 +味精) ;'與谷歌的Chrome瀏覽器,以獲得更好的調試消息 – dotoree 2012-03-27 11:02:38

+0

thankx dotoree,會嘗試,現在 – mack 2012-03-27 11:03:29

回答

0

這是因爲你打印整個對象。誤差函數實際上返回三個參數:

error(jqXHR, textStatus, errorThrown)

像這樣做,而不是:

error: function(jqXHR, textStatus, errorThrown) { 
    alert("Status:="+textStatus + " Error:="+errorThrown); 
} 
+0

狀態:=超時錯誤:=未定義 – mack 2012-03-27 11:10:56

0

發送到您的錯誤處理程序的參數是一個XHR對象。將其記錄到控制檯以檢查其值。從那裏你應該看到狀態碼和可能的錯誤。

console.log(msg); 
+0

,我已經改變了我的代碼\t'錯誤:函數(MSG){ \t \t \t \t \t \t的console.log(味精) ; \t \t \t \t \t \t onerrormsg(msg); \t \t \t \t \t}, \t \t \t \t \t成功:函數(MSG){ \t \t \t \t \t \t的console.log(MSG); \t \t \t \t \t \t onsuccessmsg(msg); \t \t \t \t \t}'現在我的問題是,在什麼地方顯示消息,console.log(msg)做了什麼? – mack 2012-03-27 11:07:01