JSON和意外字符 [英] JSON and unexpected character

查看:250
本文介绍了JSON和意外字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我从ajax调用中得到的结果:

 [
    {
        "field1": "2381",
        "field2": "1233",
        "field3": "43.79489333333333",
        "field4": "11.22697833333333"
    },
    {
        "field1": "2381",
        "field2": "1774",
        "field3": "45.70752833333334",
        "field4": "9.489278333333333"
    }
]

进行ajax调用后,它会不断返回意外字符"错误,而我使用的是这种调用:

jQuery.ajax({ 
    type: "GET",
    dataType: "json",
    url: "/myUrl.php",
    success: function(data) {
        console.log(data);
        var arrayObjects = JSON.parse(data);
    }
}); 

因此,由于我在互联网上阅读了这种Ajax调用,因此它说:

[{"value": "test"}] is valid 

所以我想知道为什么我的对象总是返回意外字符".

解决方案

通过设置dataType: "json",您告诉jQuery将接收到的数据解析为JSON,因此数据以Javascript数组的形式传递给success()函数. /p>

请参阅: http://api.jquery.com/jQuery.ajax/

Considering this result I get from an ajax call:

 [
    {
        "field1": "2381",
        "field2": "1233",
        "field3": "43.79489333333333",
        "field4": "11.22697833333333"
    },
    {
        "field1": "2381",
        "field2": "1774",
        "field3": "45.70752833333334",
        "field4": "9.489278333333333"
    }
]

and having an ajax call it keeps returning "unexpected character" error and I have this kind of call I use:

jQuery.ajax({ 
    type: "GET",
    dataType: "json",
    url: "/myUrl.php",
    success: function(data) {
        console.log(data);
        var arrayObjects = JSON.parse(data);
    }
}); 

So since I read on the internet this kind of ajax calls, it says:

[{"value": "test"}] is valid 

So I wonder why my object keeps returning "unexpected character".

解决方案

By setting dataType: "json" you are telling jQuery to parse the received data as JSON, so data is delivered to the success() function as a Javascript array.

See: http://api.jquery.com/jQuery.ajax/

这篇关于JSON和意外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆