【问题标题】:cannot convert json array to string无法将 json 数组转换为字符串
【发布时间】:2013-12-18 06:06:17
【问题描述】:

我没有收到来自服务器的任何响应,下面的代码,我收到令牌错误'

$(document).ready(function() {
    $.ajax({
        url:"url", 
        dataType: 'json',
        success: function(output) {
            var asd = JSON.stringify(output)
            var i = $.parseJSON(asd);
            for(var j=0;j<i.length;j++) {
                $('#one').append('<p><div>TITLE&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp&nbsp: &nbsp; &nbsp;<a href='+i[j].links+'>'+i[j].Title+'</a><br>SOURCE&nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+i[j].Source+'<br>CATEGORY&nbsp;: &nbsp; &nbsp;'+i[j].Category+'<hr></p></div>');
                //$('#one').append('<p><div style="background-color:#ccc"><span style="font-weight:bold" >SOURCE</span> &nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+i[j].Source+'<p>');
                //$('#one').append('<p><div style="background-color:#ccc" onclick="get"><span style="font-weight:bold" >CATEGORY</span> &nbsp;: &nbsp; &nbsp;'+i[j].Category+'<hr><p></div>');

            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.statusText);
            alert(thrownError);
        }
    });
});

【问题讨论】:

    标签: jquery ajax json arrays


    【解决方案1】:

    无需调用 JSON.stringify() 和 parseJSON()。如果 output 是一个数组,可以直接使用 output[0].Source 和 output[0].Category

    $.ajax({
    url:"url", 
    dataType: 'json' ,
    
    success:function(output) {
        for(var j=0;j<output.length;j++) {
            $('#one').append('<p><div>TITLE&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp&nbsp: &nbsp; &nbsp;<a href='+output[j].links+'>'+output[j].Title+'</a><br>SOURCE&nbsp; &nbsp;&nbsp;&nbsp; :  &nbsp; &nbsp;'+output[j].Source+'<br>CATEGORY&nbsp;: &nbsp; &nbsp;'+output[j].Category+'<hr></p></div>');
        }
    },
    error:function(xhr,ajaxOptions,thrownError){
        alert(xhr.statusText);
        alert(thrownError);
    }
    });
    

    【讨论】:

    • 它返回了意外的令牌
    • @user2992174 你能在这里发布来自服务器的原始响应吗?
    • 语法错误:意外的令牌
    • @user2992174 好的,我自己发送了这个请求。服务器还在数据末尾回答了一些评论:
    • @user2992174 服务器不应该回答任何 cmets,只回答数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2020-04-27
    • 2013-03-14
    相关资源
    最近更新 更多