2

My first attempt at it, and the testing function does not seem to work:

$.getJSON('questions.json', function(data) {alert(data);})

I am trying to alert all the contents of the JSON file which is really short. What am I doing wrong? and why am I getting [object Object]

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
ilyo
  • 35,851
  • 46
  • 106
  • 159

3 Answers3

3

JSON is a way of encoding an object as a string so that it can be passed around a network easily. When jQuery receives a string containing JSON data, it deserializes it -- it turns it back into a Javascript object. This object is passed to your success handler -- you're calling it data.

When you try to alert a Javascript object, it will give you [object Object], rather than a readable form.

You should use a Javascript console as provided by your browser to debug data like this, with the console.log method.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
2

the value of data is a JSON object, so data itself, when passed via alert(); would dump as [object Object].

Try console.log(data); instead of alert();

For my debugging and testing I use firebug, which has a nice little console tab.

1

you data is json object, so you are getting [object Object] as alert.

Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61