Skip to content Skip to sidebar Skip to footer

JSON And Unexpected Character

Considering this result I get from an ajax call: [ { 'field1': '2381', 'field2': '1233', 'field3': '43.79489333333333', 'field4': '11.226978333

Solution 1:

When using jQuery.ajax, if you specify the dataFormat, it will try to automatically parse the response according to the specified format before passing the data to the callback function.

Therefore, what you receive in your callback is not a JSON string, it's a JavaScript object already, which doesn't require any parsing.

jQuery.ajax({ 
    type: "GET",
    dataType: "json", // <-- this specifies the data format already
    url: "/myUrl.php",
    success: function(data) {
        console.log(data[0]); //logging first record
        //var arrayObjects = JSON.parse(data); //not needed

    }
}); 

Solution 2:

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/


Solution 3:

It seems the data returned from the API is already an array.

You're trying to parse an array, so the error.

so change

var arrayObjects = data;

Post a Comment for "JSON And Unexpected Character"