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]

2
  • What are you using on the server? What is questions.json returning? Commented May 18, 2011 at 17:05
  • This post answers your question: stackoverflow.com/questions/323517/… Commented May 18, 2011 at 17:08

3 Answers 3

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.

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

Comments

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.

Comments

1

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

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.