1

I'm using jQuery to grab some JSON data. I've stored it in a variable called "ajaxResponse". I cant pull data points out of it; I'm getting ajaxResponse.blah is not defined. typeof is a string. Thought it should be an object.

  var getData = function (url) {
      var ajaxResponse = "";
      $.ajax({
        url: url,
        type: "post",
        async: false,
        success: function (data) {
                ajaxResponse = data;
        }
      });
      return ajaxResponse;
  },

...

typeof ajaxResponse; // string

ajaxResponse.blah[0].name // ajaxResponse.blah is not defined 

2 Answers 2

1

make sure you specify option dataType = json

  $.ajax({
    url: url,
    type: "post",
    dataType: "json",
    async: false,
    success: function (data) {
            ajaxResponse = data;
    }
  });
Sign up to request clarification or add additional context in comments.

1 Comment

How come the type is post? would that be get? Thanx
0

Q8-coder has the right of it, but to give you some details: your server is really passing back a string that you've formatted into JSON. You'll need to tell jQuery what to expect, otherwise it just assumes it received a string.

Add the following to your $.ajax options:

dataType: "json"

Also, refer to the jQuery API for examples and documentation for these options.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.