在第1行第991行的JSON数据后出现意外的非空白字符

时间:2015-07-30 10:30:47

标签: javascript php json angularjs wordpress

我在本地有一个wordpress网站,我正在尝试使用AngularJS构建它的混合应用。

在wordpress中,我创建了一个获取数据的插件。 我将array形式的元数据作为

Array
(
[REAL_HOMES_gallery_slider_type] => Array
    (
        [0] => thumb-on-right
    )

[REAL_HOMES_property_images] => Array
    (
        [0] => 94
    )

[REAL_HOMES_property_price] => Array
    (
        [0] => 3850
    )

[REAL_HOMES_property_size] => Array
    (
        [0] => 2800
    )

[REAL_HOMES_property_bedrooms] => Array
    (
        [0] => 2
    )

[REAL_HOMES_property_bathrooms] => Array
    (
        [0] => 1
    )

[REAL_HOMES_property_garage] => Array
    (
        [0] => 1
    )

[REAL_HOMES_property_address] => Array
    (
        [0] => Athwa Gate, Surat, Gujarat 395008, India
    )

[REAL_HOMES_property_location] => Array
    (
        [0] => 21.1852392,72.80947859999992
    )

[REAL_HOMES_tour_video_url] => Array
    (
        [0] => http://vimeo.com/70301553
    )

[REAL_HOMES_featured] => Array
    (
        [0] => 1
    )

[REAL_HOMES_add_in_slider] => Array
    (
        [0] => no
    )

[REAL_HOMES_agents] => Array
    (
        [0] => 110
    )

[_dp_original] => Array
    (
        [0] => 75
    )

[REAL_HOMES_property_price_postfix] => Array
    (
        [0] => Per Month
    )

[_wp_old_slug] => Array
    (
        [0] => 15421-southwest-39th-terrace-fl
    )

[REAL_HOMES_tour_video_image] => Array
    (
        [0] => 94
    )

[REAL_HOMES_property_id] => Array
    (
        [0] => Aagam003
    )

[REAL_HOMES_property_size_postfix] => Array
    (
        [0] => Sq Ft
    )

[slide_template] => Array
    (
        [0] => default
    )

[REAL_HOMES_agent_display_option] => Array
    (
        [0] => agent_info
    )

[_edit_lock] => Array
    (
        [0] => 1436890564:1
    )

[_edit_last] => Array
    (
        [0] => 1
    )

[_thumbnail_id] => Array
    (
        [0] => 894
    )

[REAL_HOMES_slider_image] => Array
    (
        [0] => 841
    )

)

因此我将其转换为简单的array并得到了array

Array
(
[REAL_HOMES_gallery_slider_type] => thumb-on-right
[REAL_HOMES_property_images] => 94
[REAL_HOMES_property_price] => 3850
[REAL_HOMES_property_size] => 2800
[REAL_HOMES_property_bedrooms] => 2
[REAL_HOMES_property_bathrooms] => 1
[REAL_HOMES_property_garage] => 1
[REAL_HOMES_property_address] => Athwa Gate, Surat, Gujarat 395008, India
[REAL_HOMES_property_location] => 21.1852392,72.80947859999992
[REAL_HOMES_tour_video_url] => http://vimeo.com/70301553
[REAL_HOMES_featured] => 1
[REAL_HOMES_add_in_slider] => no
[REAL_HOMES_agents] => 110
[_dp_original] => 75
[REAL_HOMES_property_price_postfix] => Per Month
[_wp_old_slug] => 15421-southwest-39th-terrace-fl
[REAL_HOMES_tour_video_image] => 94
[REAL_HOMES_property_id] => Aagam003
[REAL_HOMES_property_size_postfix] => Sq Ft
[slide_template] => default
[REAL_HOMES_agent_display_option] => agent_info
[_edit_lock] => 1436890564:1
[_edit_last] => 1
[_thumbnail_id] => 894
[REAL_HOMES_slider_image] => 841
)

Ihen我使用Php将此array编码为Json。

json_encode($result);

$ result是结果数组

我使用AngularJS调用了Ajax请求,响应给了我下面的结果。

{"REAL_HOMES_gallery_slider_type":"thumb-on-right","REAL_HOMES_property_images":"94","REAL_HOMES_property_price":"3850","REAL_HOMES_property_size":"2800","REAL_HOMES_property_bedrooms":"2","REAL_HOMES_property_bathrooms":"1","REAL_HOMES_property_garage":"1","REAL_HOMES_property_address":"Athwa Gate, Surat, Gujarat 395008, India","REAL_HOMES_property_location":"21.1852392,72.80947859999992","REAL_HOMES_tour_video_url":"http:\/\/vimeo.com\/70301553","REAL_HOMES_featured":"1","REAL_HOMES_add_in_slider":"no","REAL_HOMES_agents":"110","_dp_original":"75","REAL_HOMES_property_price_postfix":"Per Month","_wp_old_slug":"15421-southwest-39th-terrace-fl","REAL_HOMES_tour_video_image":"94","REAL_HOMES_property_id":"Aagam003","REAL_HOMES_property_size_postfix":"Sq Ft","slide_template":"default","REAL_HOMES_agent_display_option":"agent_info","_edit_lock":"1436890564:1","_edit_last":"1","_thumbnail_id":"894","REAL_HOMES_slider_image":"841"}0

我不知道为什么即使我没有在响应中添加任何内容,最后也会有零。 我猜结果是一个Json字符串,所以我尝试将其转换为Json对象以使用它并从键中获取数据。 我还有[]括号中的其他Json响应。但是这个不是,可能是由于Json字符串响应结束时的0。

请帮帮我。 感谢。

1 个答案:

答案 0 :(得分:2)

这是wordpress及其ajax系统的一个问题。如果查看文件wp-admin/admin-ajax.php,在文件的末尾会有一个die(0)语句,这是罪魁祸首。

为了不运行该语句,您需要添加一个wp_die();输出JSON字符串后的语句(这在wordpress documentation on ajax requests中)。