2012-08-02 131 views
0

我遇到了ajax jquery函數的問題。這是可能的javascript代碼:ajax成功響應問題

$.ajax({ 
    type: "POST", 
    url: "login.php", 
    data: { 
    username: username, 
    password: password 
    }, 
    dataType: "text", 
    contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15", 
    success: function (msg) { 
    $("#button").ajaxComplete(function (event, request) { 
     alert(msg == 'ok'); 
     if (msg == 'ok') { 
     $('#load').show(); 
     setTimeout(function() { 
      $("#username").css("background", "green"); 
      $("#password").css("background", "green"); // add red color 
     }, 1000) 
     setTimeout("submitForm()", 2000); 
     } else { 
     $('#load').show(); 
     setTimeout(function() { 
      $("#username").css("background", "red"); 
      $("#password").css("background", "red"); // add red color 
      $('#load').hide(); 
      $('#button').show(); 
     }, 1000); 
     } 
    }); 
    } 
}); 

我不明白爲什麼,如果(MSG ==「確定」)返回false,即使在對象「味精」有一個字符串「OK」。

+0

哪裏'msg'從何而來? – 2012-08-02 20:12:25

+0

爲什麼ajaxcomplete在成功處理函數裏面? – Shyju 2012-08-02 20:13:43

+0

繼承人雅的http://jsbeautifier.org/不錯的鏈接。理解你越是在你的問題上工作,越多的人將在答案上工作...... – elclanrs 2012-08-02 20:14:13

回答

0

IThe成功事件需要具有三個參數的功能:
data, textStatus, andjqXHR。我假設「ok」是由login.php給出的響應,在這種情況下,這將是data參數的值。因此請檢查數據參數以確定OK的值。

您也可能只想要alert(data);,以查看究竟返回的是什麼。

參見:http://api.jquery.com/jQuery.ajax/

$.ajax({ 
    type: "POST", 
    url: "login.php", 
    data: { 
     username: username, 
     password: password 
    }, 
    dataType: "text", 
    contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15", 
    success: function(data, textStatus, jqXHR) { 
     alert(data == 'ok'); 
     if (data == 'ok') { 
      $('#load').show(); 
      setTimeout(function() { 
       $("#username").css("background", "green"); 
       $("#password").css("background", "green"); // add red color 
      }, 1000) 
      setTimeout("submitForm()", 2000); 
     } else { 
      $('#load').show(); 
      setTimeout(function() { 
       $("#username").css("background", "red"); 
       $("#password").css("background", "red"); // add red color 
       $('#load').hide(); 
       $('#button').show(); 
      }, 1000); 
     } 
    } 
}); 
+0

我試過你的代碼,但它不工作。即使「msg」對象包含一個字符串,並且這個字符串實際上是「ok」(我已經看到它使用Firebug),它總是返回false。它很棒在另一個項目中,相同的文件login.php和腳本jQuery的工作!! Somone可以幫助我?謝謝! – 2012-08-02 20:58:59

+0

'data'的實際值是多少?這可能會幫助你弄清楚發生了什麼事。 – 2012-08-02 22:49:17

+0

數據的實際值爲「ok」 – 2012-08-03 11:53:55