I am using a ajax request with the following call to function.
var test = initBuild(id);
test.success(function (data){
console.log(data);
});
The data being returned looks like
{"built_when":{"id":"43701","clientId":"245","name":"Big Ass Gallery","productId":"0","desc":null,"bgColor":"#000000","fontColor":"ffffff","sort":"43701","clientRef":"205","isFeatured":"0","created":"1367356190","views":"0","finish":"0","isArchive":"0","showMailShare":"0","hardPageFlip":"0","hardCoverFlip":"0","isTemp":"0","agreement":"0","maxChange":"0","countChange":"0","verticalFlip":"0","musicSwitch":"0","showFbShare":"0","twitter":null,"email":"0","pageHeight":"1000","pageWidth":"1391"}}
I am trying to access by data.built_when.id, but it's returning null. What am I missing on this one.
Thanks
asked May 1, 2013 at 2:57
SuperNinja
1,6167 gold badges23 silver badges45 bronze badges
2 Answers 2
It's being returned as a string. Use data = $.parseJSON(data); to convert it to JS constructs. Alternatively, you could set the Content-type: application/json header on the server side when you emit the JSON
answered May 1, 2013 at 3:00
Explosion Pills
193k56 gold badges341 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Arun P Johny
or set
dataType: 'json' in the ajax optionSuperNinja
@Explosion Pills -- thanks for the answer. Works perfectly. Added the dataType to the function and took care of it that way. should have posted the function.
If you are sure about the return type of the ajax request to be always json then you can set dataType: 'json' to the ajax option.
Ex:
$.ajax({
url: '',
dataType: 'json',
....
})
answered May 1, 2013 at 3:11
Arun P Johny
389k68 gold badges533 silver badges532 bronze badges
Comments
lang-js
var dataObject = JSON.parse(data)if not.