2009-09-12 70 views
4

服務器端:response.statusText = 「OK」,而是落入錯誤回調從阿賈克斯與jQuery

.... 
    $_SESSION['accountId'] = $accountId; 
    $_SESSION['type'] = $_POST['accountType']; 
    echo '1'; 
    return; 

客戶端:

$.ajax(
{ 
    type:'POST', 
    data: $("#flowFormSubmit").serialize(), 
    dataType:'text/plain', 
    timeout:1000, 
    success:function(response){ 
     if(-1 == response) 
      alert('fail'); 
     else 
      alert('succeed'); 
    } 
}); 

我測試過它的權利,在「迴歸停止;」 在什麼情況下不會調用成功函數?

編輯

添加錯誤回調後

,它抓住了,但沒有輸出有用的信息尚未:

error:function(response){ alert(response);alert(response.statusText); }, 

它只輸出:

[object XMLHttpRequest] 
OK 

爲什麼它陷入錯誤回調?

+0

請添加錯誤回調。它可能正在解僱,並可能有助於揭示這個問題。 – 2009-09-12 03:42:59

+0

服務器端代碼中的return語句是否必需? – 2009-09-12 04:02:48

+0

是的,它是必要的。並且服務器端操作完成,爲什麼它仍然陷入錯誤回調? – omg 2009-09-12 04:06:10

回答

3

這可能是指定URL option一個好主意:

$.ajax(
{ 
    url:'foo.php', 
    type:'POST', 
    ... 
+0

優秀點 – 2009-09-12 03:45:51

+1

令人驚訝的是他得到'回報' – 2009-09-12 03:47:34

+0

收件人URL可能與請求URL相同,所以jQuery可能使用合理的默認值(即當前頁面)。 – 2009-09-12 03:49:10

0

你忘了:

header('Content-Type:text/plain'); 

在服務器上?

+0

不,仍然沒有迴應。 – omg 2009-09-12 03:54:40

0

1)轉至該網址在瀏覽器中(你與AJAX獲取)

2)設置jQuery的Ajax請求的錯誤回調的一個。

在你的PHP代碼
0

,使用json_encode返回響應

如:

$response = array('success'=>1); 
echo json_encode($response); 

然後在阿賈克斯成功的功能應該是

if (response.success == 1) { alert('success'); } 
1

您忽略了從return語句服務器端代碼。至於你的錯誤回調而言,使用下面的通用AJAX回調代碼,讓我們知道了什麼狀況,responseText的,和狀態文本屬性時失敗:

function callback(response) 
{ 
    if (response.readyState == 4) 
    { 
    if (response.status == 200) 
    { 
     alert("Response Success:\n" + response.responseText); 
    } 
    else 
    { 
     alert("Response Error:\n" + response.statusText); 
    } 
    } 
} 
+0

真的很奇怪,成功回調有時會被調用,有時候不會。 – omg 2009-09-12 04:23:17

3

有更多有用的參數錯誤回調。嘗試獲取textStatus。

error: function(XHR, textStatus, errorThrown) { console.log(textStatus); } 

檢查是什麼。

一個快速猜測是,你的意思是使用dataType: 'text'而不是'text/plain'。 (Check documentation

$.ajax({ 
    type:'POST', 
    data: $("#flowFormSubmit").serialize(), 
    dataType:'text', 
    timeout:1000, 
    success:function(response){ 
     if(-1 == response) 
       alert('fail'); 
     else 
       alert('succeed'); 
    } 
});