Jquery - Ajax: SyntaxError: JSON.parse: unexpected character

后端 未结 3 1598
闹比i
闹比i 2021-01-27 09:36

I have the following code and output, and I cant fine any error on the json data to be decoded. Can anyone please help me in finding this error

CODE:

            


        
相关标签:
3条回答
  • 2021-01-27 10:15

    When you specify dataType : json, result is already parsed in 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);

    0 讨论(0)
  • 2021-01-27 10:21

    data returned already json format only,

        $.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);
               });
    
           }
       });
    
    0 讨论(0)
  • 2021-01-27 10:23

    jQuery is clever enough to parse the response as it is, even if dataType isn't specified.

    In your case, it is specified and therefore, it is already parsed and data is the parsed JSON object.

    What you are doing is therefore parsing an Object.

    Doc says:

    dataType: (default: Intelligent Guess (xml, json, script, or 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). The available types (and the result passed as the first argument to your success callback) are:

    0 讨论(0)
提交回复
热议问题