简体   繁体   English

Jquery - Ajax:SyntaxError:JSON.parse:意外字符

[英]Jquery - Ajax: SyntaxError: JSON.parse: unexpected character

I am receiving the following valid JSON from my server:我从我的服务器收到以下有效的 JSON:

[{"name":"Bubble Witch Saga 2","impressions":10749},{"name":"Grinder","impressions":11284},{"name":"Loovoo","impressions":12336},{"name":"Injustice God Among Us","impressions":12786},{"name":"Bookmyshow","impressions":13182},{"name":"Angry Bird","impressions":15404},{"name":"Flipkart","impressions":16856},{"name":"CNN-IBN","impressions":17230},{"name":"Fore Square","impressions":17595},{"name":"NDTV","impressions":19542},{"name":"Whatsapp","impressions":19976}]

But I keep getting an error in my console saying "JSON.parse: unexpected character."但是我的控制台中不断出现错误,提示“JSON.parse:意外字符”。 This is my client-side code:这是我的客户端代码:

$.ajax({
    'type': 'get',
    'data': {},
    'dataType': 'json',
    'url': 'dashboard/data/'+type,
    'complete': function(data) {
        var top10Value = JSON.parse(data);
        $.each(top10Value, function(key,value){
            console.log(key+" -- "+value);
        });
    }
});

Why am I getting this error?为什么我收到这个错误?

jQuery is clever enough to parse the response as it is, even if dataType isn't specified. jQuery 足够聪明,可以按原样解析响应,即使未指定dataType

In your case, it is specified and therefore, it is already parsed and data is the parsed JSON object.在您的情况下,它是指定的,因此它已经被解析, data是解析的 JSON 对象。

What you are doing is therefore parsing an Object .因此,您正在做的是解析Object

Doc says: Doc 说:

dataType : (default: Intelligent Guess (xml, json, script, or html))数据类型:(默认:智能猜测(xml、json、脚本或 html))

Type : String类型:字符串

The type of data that you're expecting back from the server.您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object , in 1.4 script will execute the script, and anything else will be returned as a string).如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它(XML MIME 类型将产生 XML,在 1.4 JSON 将产生一个 JavaScript 对象,在 1.4 脚本中将执行脚本,其他任何内容都将以字符串形式返回)。 The available types (and the result passed as the first argument to your success callback) are:可用类型(以及作为第一个参数传递给成功回调的结果)是:

data returned already json format only,仅返回 json 格式的数据,

    $.ajax({
       'type': 'get',
       'data': {},
       'dataType': 'json',//Return Json Format
       'url': 'dashboard/data/',
       'complete': function(data) {
           //data returned already json format only
           //var top10Value = JSON.parse(data);
           $.each(top10Value, function(key,value){
               console.log(key+" -- "+value);
           });

       }
   });

When you specify dataType : json, result is already parsed in jQuery.当您指定 dataType : json 时,结果已在 jQuery 中解析。

Moreover, complete function argument is returning an object representing the result not the result itself.此外,完整的函数参数正在返回一个表示结果的对象,而不是结果本身。

That case you should use var top10Value = JSON.parse(data.responseText) ;在这种情况下,您应该使用var top10Value = JSON.parse(data.responseText) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM