$.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字符串,然后在响应中回显,如下所示:
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调用上面,我得到response: [object Object]
的响应值与Firebug中的空对象。
为什么我不会收到响应返回到我的jQuery代码与json编码的字符串?
正如Felix King在上面的评论中所提供的那样,这实际上是由于使用:
将resp
转换为字符串造成的:console.log('response: ' + resp)
改变:
console.log(resp);
解决了问题