I have the below ajax which is returning the data as {"param1": "value1"}. When I try to print the value of param1 by using data.param1, it returns undefined. There is only one value in the data. Am I doing anything wrong here?
$.ajax({
'type': "POST",
'url': "${someresourceURL}",
// data received is [{"param1": "value1"}]
'success': function (data) {
console.log(data); // returns [Object Object]
console.log(data.param1); returns undefined
}
});
3
Answers
seems to work ok, do you need to deserialize the response?
it must be parsed using JSON before you can use it as JS Dictionary
Route 1 (Manually doing it)
Route 2 (Signify Return is JSON from the Backend)
JSON.parse()
automatically and data will be a dictionaryPHP
Python/Django
you’re missing
dataType: 'json'