JSON编码与Services_JSON和json_encode在PHP返回空对象与jQuery ajax调用


JSON encoding with Services_JSON and json_encode in PHP returning empty object with jQuery ajax call

            $.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);

解决了问题