AJAX json unexpected token '

后端 未结 2 814
太阳男子
太阳男子 2021-01-20 04:26

I have this code:

    $.ajax({
            dataType: \'text\',
            url: \'/_/js/answers.json\',
            type: \"GET\",
            success: funct         


        
相关标签:
2条回答
  • 2021-01-20 04:32

    The ' characters around your JSON make it a JavaScript string and don't form part of the data.

    It looks like you have those characters in the JSON that you are requesting over HTTP so there they do form part of the data.

    This is not valid JSON. Remove the quotes.

    You should have:

    {"code": 123}
    

    Not

    '{"code": 123}'
    
    0 讨论(0)
  • 2021-01-20 04:51

    Try changing dataType to JSON:

    $.ajax({
        dataType: 'JSON',
        url: '/_/js/answers.json',
        type: "GET",
        success: function (data) {
            alert(data);
            alert(data.code);
            var result = JSON.parse(data);
            var hey = JSON.parse('{"code": 123}'); 
            alert(hey.code);
            alert(result.code);
        },
        error: function () {
            alert("code not found");
        }
    });
    
    0 讨论(0)
提交回复
热议问题