Reputation:
I have this function:
function returnVehicleByValue(vehicleValue){
$.get("inc/ajax/selectMojaVozila.php",
{vehicleValue:vehicleValue},
function(html){
var vehicle = JSON.parse(html);
}
);
}
But when I execute it it gives me the error
Uncaught SyntaxError: Unexpected token A
It comes from the line:
var vehicle = JSON.parse(html);
I don't understand why I checked everything and it seems alright.
"html" returns an array I checked that When I say alert(html) it pops up "Array"
Upvotes: 1
Views: 5156
Reputation: 119847
Because jQuery automatically parses the return. If it determines it to be JSON, it will parse it to JSON. You can check this by doing console.log(typeof html)
in your callback. If it's pre-parsed by jQuery, it prints object
.
From $.get()
documentation:
dataType
Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, or html).
Upvotes: 3
Reputation: 7562
When you use the $.get function you don't have to parse the response - it will be parsed automatically.
Upvotes: 0