I have a JSON response in JavaScript that looks like this:
{"opt1":"2.92","opt2":"4.24","opt3":"6.36"};
This is the result of applying console.log(data) where data is the response:
success: function(data){
console.log("Ajax succeeded");
console.log(data);
console.log(data.opt1);
},
However, I cannot access the value of opt1. The console indicates undefined. Any ideas why this could be the case? The JSON is valid, I checked it.
EDIT
The server in this case uses the json_encode function in PHP to convert an array to JSON. I have tried removing the semicolon at the end, this still gives me an undefined value for opt1. I also tried setting the dataType of the ajax call, that results in an error, indicating there is an unexpected character.
console.log(data)isn't logging the JSON string as a string? If you didn't explicitly tell jQuery that you're expecting JSON it tries to guess what format the response is, and I would guess that since your string is not valid JSON it is not parsing it for you so you can't access properties.dataType: "json"in the AJAX call, or is the server sendingContent-type: application/json?