簡體   English   中英

Wordpress后端中的JSON.parse返回錯誤

[英]JSON.parse in Wordpress Backend returns error

我有一個WordPress頁面的下拉列表,其類別為“ page_id”(它們可以重復並可以添加..因此是livequery)。 在更改事件上,我想發送一個ajax請求,以拔出所選頁面的標題,摘錄和縮略圖。 目前,我只是想獲取任何有效的json響應。 我在回調中使用json_encode將json對象返回給我的.ajax函數。 然后在.ajax成功函數中,我試圖將此json對象解析為可用的對象,但每次都會出現錯誤。

該錯誤似乎是JSON.parse的問題:

JSON.parse
success(response="{"title":"bacon title","message":"bacon text"}0")   
handleError(a=Object { url="http://localhost/single/wp-admin/admin-ajax.php", global=true, more...}, b=XMLHttpRequest { onreadystatechange=[xpconnect wrapped nsIDOMEventListener], readyState=4, more...}, d="success", e="{"title":"bacon title","message":"bacon text"}0")    onreadystatechange(m=readystatechange )
[Break On This Error] var json = JSON.parse(response); 

firebug控制台顯示響應如下:

{"title":"bacon title","message":"bacon text"}0

我不知道那額外的0是否是麻煩制造者,但我也不知道它是如何到達那里的。 chrome將錯誤報告為:

Uncaught SyntaxError: Unexpected number
$.livequery.$.change.$.ajax.success/single/wp-admin/post.php?post=2&action=edit&message=1:917

這是我的jQuery函數:

<script type="text/javascript">
//<![CDATA[
jQuery(function($) {

    $('.page_id').livequery(function(){ 

        var loading = $(this).next('img.ajax-loader');


        $(this).change( function() {

          var value = $(this).val();
          if (value.length) {

            $(loading).fadeIn();

            var data = {
            action: 'featured_meta_action',
            data: value,
            security: '<?php echo wp_create_nonce('featured-ajax-nonce'); ?>',
            };

            $.ajax({
                type: "POST",
                data: data,
                url: ajaxurl, 
                complete: function(){
                    $(loading).fadeOut();
                    },
                success: function(response){
                    var str = '{ "title": "bar" }';
                    var json = JSON.parse(response); 

                    alert(json['title']);
                },
                error: function(){
                    alert('fail');
                }
            });
          }//end if
        }); //end change

    }); //end livequery


}); //end ready
/* ]]> */
</script>               

我的PHP回調看起來像:

function featured_meta_action_callback(){

    $responseVar = array(
                    'title'=>'bacon title',
                    'message'=>'bacon text',
                    );

    echo json_encode($responseVar); 

}
add_action('wp_ajax_featured_meta_action', 'featured_meta_action_callback');

事實證明,這比看起來要難得多。 我只需要從PHP函數獲取數據,以便可以在jquery中使用它。 我要去哪里錯了?

我認為可能是這樣的:

function featured_meta_action_callback(){

    $responseVar = array(
                    'title'=>'bacon title',
                    'message'=>'bacon text' // <<< Here you do not need another comma
                    );

    echo json_encode($responseVar); 

}

請參閱數組中的注釋。

編輯:盡管沒有必要,但在測試中,如果我將其保留在JSON對象的末尾,則不會輸出0。 因此,它很可能來自其他地方(換句話說,就是WordPress代碼)。

編輯v2:通過Firebug的控制台運行它們。 第二個引發錯誤,但是如果您不包含0,則可以正常工作。

console.log(JSON.parse('{"title":"bacon title","message":"bacon text"}'));
console.log(JSON.parse('{"title":"bacon title","message":"bacon text"}0'));

編輯v3:好的,您顯然需要通過在函數末尾調用exit / die來縮短WP進程的其余部分,如下所示:

function featured_meta_action_callback(){

    $responseVar = array(
                    'title'=>'bacon title',
                    'message'=>'bacon text' // <<< Here you do not need another comma
                    );

    echo json_encode($responseVar); 

    exit;
}

建議在這里:

http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Administration_Side

和這里:

http://amiworks.co.in/talk/simplified-ajax-for-wordpress-plugin-developers-using-jquery/

暫無
暫無

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

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