我正在使用jQuery的ajax方法將一些數據發佈到服務器並取回響應。雖然服務器端php代碼正在返回一個json編碼的字符串/數組,但響應會返回爲空。PHP函數不返回任何數據給JQuery ajax方法
有人能指出我犯的錯誤嗎?下面是如何使用我正在觸碰postData.php頁面的jquery ajax方法。
$.ajax({
url:'postData.php',
type:'POST',
data:data,
dataType: "json",
success: function(response){
console.log(response);
}
});
postData.php中的內容非常直接,因爲我仍在開發它。
$data = array();
//inside postData.php
$data['test']=1;
return json_encode($data);
它應該返回一個json字符串,但它返回null。我也嘗試在$ data數組聲明後面回顯一個字符串,它在firebug中回顯它,但是響應是當我在成功回調中執行console.log時,它會返回爲空。
謝謝!!我已經使用jquery和ajax了很多,並且急於快速完成任務,我犯了這個有趣的錯誤! – macha 2011-05-23 01:00:29