2013-03-06 127 views
0

我有這樣的代碼:簡單的jQuery/AJAX錯誤

var custID = 1; 

$.ajax({ 
    url: 'php/viewCustomer.php', 
    type: 'GET', 
    data: '{custID: ' + custID + '}', 
    dataType: 'json', 
    cache: false, 
    beforeSend: function() { 
     $('#display').append('<div id="loader"> Lodaing ... </div>'); 
    }, 
    complete: function() { 
     $('#loader').remove(); 
    }, 
    success: function (data) { 
     //do something 
    }, 
    error: function() { 
     alert('could not process'); 
    } 
}); 

存在錯誤和警告該錯誤消息could not process,所以我試圖調試它是這樣的:

var custID = 1; 

$.ajax({ 
    url: 'php/viewCustomer.php', 
    type: 'GET', 
    data: '{custID: ' + custID + '}', 
    dataType: 'json', 
    cache: false, 
    beforeSend: function() { 
     $('#display').append('<div id="loader"> Lodaing ... </div>'); 
    }, 
    complete: function() { 
     $('#loader').remove(); 
    }, 
    success: function (data) { 
     //do something 
    }, 
    error: function (jqXHR) { 
     alert('Error: ' + jqXHR.status + jqXHR.statusText); 
    } 
}); 

,其輸出:

200 OK 

所以,如果它是好的,爲什麼地球上它執行錯誤:函數。困惑,請幫助。

+2

我相信你可以編輯它,如果它太困擾你了 – 2013-03-06 17:35:29

+1

我的觀點是編輯,OP會看到你正在編輯並理解他在釣魚時很糟糕。 – 2013-03-06 18:00:13

回答

1

如果您打算將它作爲JSON對象,則數據字符串格式不正確。有關於這個前一個問題:Jquery passing data to ajax function

相反,嘗試:

data: JSON.stringify({custID: custID}), 

格式爲(鑰匙):(可變)。我之前的回答是在變量周圍加上引號,這是不必要的。

+0

謝謝,我試過了,但仍然出現錯誤。 – 2013-03-06 17:42:49

+0

我的不好。我改變了我的答案。 – Terry 2013-03-06 18:01:41

+0

我自己對錯誤進行了排序。我是jQuery的初學者。我的PHP文件沒有迴應任何json。 – 2013-03-06 18:05:21