0

Here's my JS code from a success section of an AJAX call:

success: function(msg){
    var data = JSON.parse(JSON.stringify(msg));
    $("#searchResults").html(data + " Value for 'a': " + data.color + "\nValue for 'b': " + data.message);
}

Here's what's printed on the page:

{"color":"Yellow","message":"Pending"} Value for 'a': undefined Value for 'b': undefined

Why are they undefined?

1
  • 4
    Remove the JSON.stringify. Commented Jan 22, 2015 at 14:44

2 Answers 2

1

data in the console.log seems to be a string still, like @Musa said, you need to remove the JSON.stringify

Sign up to request clarification or add additional context in comments.

Comments

0

If your ajax response is JSON you should explicitly state so in the request so that it will be parsed automatically for you by jQuery.ajax . dataType: 'json'

$.ajax({
...
    dataType: 'json',
    success: function(data){
        $("#searchResults").html(" Value for 'a': " + data.color + "\nValue for 'b': " + data.message);
    }
...

Comments

Your Answer

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.