簡體   English   中英

SyntaxError:JSON中位置1處的意外令牌s

[英]SyntaxError: Unexpected token s in JSON at position 1

我正在制作一個插件。 我試圖通過將數組/對象/ json數組發布到此php代碼來簡單地更新Wordpress中的發布元:

public static function save_settings_fields(){

    $myArray = json_decode($_POST['to_save']);
    wp_send_json(var_dump($_POST['to_save']));

    //i can't even get to this point.
    $saved_content = json_decode($_POST['to_save']);
    $post_id = $saved_content['post_id'];
    $this_count = (int)$saved_content['index'];
    $foobar = new POB_Save_Settings;
    $isdone = $foobar->update_static_content($post_id, $this_count, $saved_content);
    if ( is_wp_error( $isdone ) ) {

        wp_send_json_error( $isdone );

    } else {
      wp_send_json( $isdone );
    }
  }

我的腳本是:

var SaveOptionSettings = function(postid, count){
    var save_array = new Array();
    save_array[0]= {};
    save_array[0]['post_id'] = postid;
    save_array[0]['content_count'] = count;
    save_array[1] = {};
    for(var i=0; i<$('#settings input').length; i++){
      save_array[1][$('#settings input').eq(i).attr('id')] = $('#settings input').eq(i).val();
    }
    for(var i=0; i<$('#settings textarea').length; i++){
      save_array[1][$('#settings textarea').eq(i).attr('id')] = $('#settings textarea').eq(i).val();
    }
    for(var i=0; i<$('#settings select').length; i++){
      save_array[1][$('#settings select').eq(i).attr('id')] = $('#settings select').eq(i).val();
    }
    console.log(save_array, JSON.stringify(save_array));
    var pva_ajax_url = pva_params.pva_ajax_url;

    $.ajax({
        type: 'POST',
        url: pva_ajax_url,
        dataType: 'json',
        data: {
            action: 'save_settings_fields',
            'to_save': JSON.stringify(save_array)
        }, success: function (result) {
          M.toast({html: 'Saved!'})
        },
        error: function(xhr,textStatus,err)
        {
            console.log("readyState: " + xhr.readyState);
            console.log("responseText: "+ xhr.responseText);
            console.log("status: " + xhr.status);
            console.log("text status: " + textStatus);
            console.log("error: " + err);
        }
    });
  }

wp_send_json幾乎將$_POST得到的信息發回給我,這顯然是:

readyState: 4
responseText: 
string(202) "[{\"post_id\":15404,\"content_count\":1},{\"label\":\"Options <span>(please select one)</span>\",\"use_conditionals\":\"on\",\"classes\":\"form-buttons\",\"style_id\":\"options\",\"subtitle\":\"test\"}]"
null
status: 200
text status: parsererror
error: SyntaxError: Unexpected token s in JSON at position 1

我嘗試過的

  • 在發布的信息上使用json_decode
  • contentType: "application/json"到AJAX
  • 刪除Stringify,dataType,將dataType更改為Text的所有組合
  • 使每個變量都被.push到數組中
  • 使數組成為對象,反之亦然

難道我做錯了什么?

您的服務器響應以字符串(202)開頭,這不是有效的json

string(202) "[{\"post_id\":15404,\"content_count\":1},{\"label\":\"Options <span>(please select one)</span>\",\"use_conditionals\":\"on\",\"classes\":\"form-buttons\",\"style_id\":\"options\",\"subtitle\":\"test\"}]"
null

error: SyntaxError: Unexpected token s in JSON at position 1 

這是來自String

您的responseText應該類似於

[{
    "post_id": 15404,
    "content_count": 1
}, {
    "label": "Options <span>(please select one)</span>",
    "use_conditionals": "on",
    "classes": "form-buttons",
    "style_id": "options",
    "subtitle": "test"
}]

//更新

@ lawrence-cherone php端提及

var_dump()

wp_send_json(var_dump($_POST['to_save']));

此行需要刪除或評論

感謝@ lawrence-cherone

另外,如果您想調試內容,則使用echoprint_r可能會獲得更好的成功。

使用這些版畫的一個無論你進入到頁面上,然后將其返回的內容result在AJAX success: function(result){ ... }

暫無
暫無

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

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