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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序