1

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
1
  • Have you parsed the data into an object? Try var dataObject = JSON.parse(data) if not. Commented May 1, 2013 at 2:59

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

or set dataType: 'json' in the ajax option
@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.
1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.