drfm55597 2019-07-02 09:13
浏览 144
已采纳

将数组从php传递到ajax时,位置0的JSON中出现意外的标记C.

I am trying to send array from php to ajax in json file but when i alert res var for testing it i see this error message :

Uncaught SyntaxError: Unexpected token C in JSON at position 0

My array is this :

["C", "Dbm", "Bb", "Bb", "F", "Cm", "Eb", "Dbm", "Bb", "Bb", "F", "Cm", "F", "Bb", "Eb", "Bb", "F",…]

My array created by php function and array item's will different when user clicking on a button in view .

Java Script :

$(".T-chords").on('click',function(event){

    event.preventDefault();
    var This = $(this);
    $.ajax({

        url : data.ajax_url,
        type : 'post',
        dataType: 'json',
        data : {

            action : 'transpose_callback',
            content : data.content,
            target_scale : This.text(),
            base_scale : data.base_scale,
        },

        success:function(response){

            var res = JSON.parse(response);
            alert(res[1]);

        },

        error: function(){

            alert("err");

        }


    })

})

php code :

function Ajax_transpose_callback(){
    header('Content-Type: application/json');
    $content = $_POST['content'];
    $Target_Scale = $_POST['target_scale'];
    $Base_Scale = $_POST['base_scale'];
    $Flag_db = "";
    $transposed_chord = "";
    $transposed_arr = array();

    if(preg_grep('/#/', $content)){
        $Flag_db = "0";
    }
    elseif (preg_grep('/b/', $content)){
        $Flag_db = "1";
    }
    else{
        $Flag_db = "0";
    }

    foreach ($content as $item) {

        $final_item = substr( $item, 1, - 1 );
        $transposed_arr[] = Transpose( $Flag_db, $Base_Scale, $Target_Scale, $final_item );

    }
    wp_die(json_encode($transposed_arr));
}
  • 写回答

2条回答 默认 最新

  • dongwei5740 2019-07-02 09:17
    关注

    This is either because you are parsing a already parsed object. Try to remove var res = JSON.parse(response); and change it to var res = response;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line