簡體   English   中英

PHP中使用Services_JSON和json_encode進行JSON編碼,並使用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中帶有空對象。

為什么我不會收到帶有JSON編碼字符串的對jQuery代碼的響應?

正如Felix King在上面的評論中提供的,這實際上是由於將resp強制轉換為一個字符串而導致的:

console.log('response: ' + resp)

更改為:

console.log(resp);

解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM