drfm55597 2019-07-02 01: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 01: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥20 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥15 alpha101因子里哪些适合crypto?