views:

44

answers:

2
+1  Q: 

cant query json

Im using jquery to grab some json data, ive stored it in a var called "ajaxResponse". I cant pull data points out of it, im 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
+1  A: 

make sure you specify option dataType = json

  $.ajax({
    url: url,
    type: "post",
    dataType: "json",
    async: false,
    success: function (data) {
            ajaxResponse = data;
    }
  });
Anwar Chandra
aw man! thanks :) my bad, dont know where that went to
alan
A: 

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.

James van Dyke