0
$.ajax({
url: '/ajax/get_rule.php',
dataType: 'json',
data: {
feature : feature,
ajax : 1
},
success: function(resp) {
console.log('in here');
console.log('response: ' + resp);
},
error : function() {
console.log('there was an error...');
}
});
我使用PHP PEAR Services_JSON類到我的陣列編碼成JSON字符串,然後回聲表示出來的響應,這樣的:JSON編碼與Services_JSON和json_encode在PHP返回空對象與jQuery AJAX調用
echo Registry('json')->encode(array('ajax' => 1, 'feature' => 'rfc1872'));
exit;
在上面的例子中Registry('json')
是Services_JSON
對象。
我也曾嘗試PHP內置函數json_encode
:
echo json_encode(array('ajax' => 1, 'feature' => 'rfc1872'));
exit;
無論這些工作,雖然我不屬於在jQuery的Ajax調用錯誤回調上面我得到的響應值在Firebug中有一個空對象的response: [object Object]
。
爲什麼我沒有收到迴應我的jQuery代碼與JSON編碼的字符串?
你怎麼知道它是空的?我沒有看到你將它記錄到控制檯。 'console.log('response:'+ resp);'首先將對象轉換爲字符串,而對象的默認字符串表示形式爲'[object Object]「,無論它是否具有屬性。 'console.log(resp);'顯示了什麼?如果你沒有得到JSON作爲迴應,我相信甚至不會執行成功回調。 – 2013-04-05 09:34:07
呵呵,這正是它的原因。我改變了'console.log('response:'+ resp);'到'console.log(resp);',正如你所建議的,並且對象正確地拉動。非常感謝,你能否提交答案,我會很樂意接受。感謝您指出了這一點。 – crmpicco 2013-04-05 09:44:58