0

I'm fairly new to JQuery. The code below works and I can see the correct JSON response in Firebug. But I couldn't find a way how to get and parse it in the code. Alert window only shows "[object Object]" but not any json text.

<script>

$.ajaxSetup({ cache: false });

var _token;

function make_token_auth(user, token) {
  var tok = user + ':' + token;
  return "Token " + tok;
}


$.ajax
  ({
    type: "GET",
    url: "url",
    dataType: 'json',    
    beforeSend: function (xhr){ 
        xhr.setRequestHeader('Auth', make_token_auth('userid', 'token')); 
    },
    success: function (data){
        alert(data); 
    }
});

</script>
2
  • 1
    What about console.log(data); ? Commented Jan 7, 2013 at 15:57
  • 3
    Use console.log(data) instead of alert() in a good browser (not IE) you'll see the properties of the data object fully itemised in the console. Commented Jan 7, 2013 at 15:57

5 Answers 5

3

The fact you precised

dataType: 'json',    

tells jQuery to parse the received answer and give it as a javascript object to your success callback.

So what you have here is fine and what is alerted is correct (this is an object, so alert simply prints the result of data.toString()).

Use console.log to see what it is exactly :

success: function (data){
    console.log(data); 
}

and open the developer tools in Chrome or the console in Firebug to browse the properties of the object.

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

1 Comment

he's already specified that he's using Firebug, so not sure why you're linking to the Chrome developer tools?
3

don't use alert() for debugging -- it's often unhelpful (as in this case), and also has serious issues when used with asyncronous code (ie anything Ajax) because it interrupts the program flow.

You would be much better off using the browser's console.log() or console.dir() functions, and seeing the object displayed in the console. It is much more functional, and doesn't interrupt the flow of the program.

So instead of alert(myjsonvar) use console.log(myjsonvar).

2 Comments

where can I see the console output?
@spring - in Firebug. See the "Console" tab at the top of the firebug window?
1

You can get the json string by using JSON.stringify

var jsonstr = JSON.stringify(data);
alert(jsonstr);

Comments

1

The alert function expects you to pass in a string or number.

Try doing something like this:

for(x in data) {
    alert(x + ': ' + data[x]);
}

Update in response to comments: You can use alert in development or production to see string and number values in the object returned by the server-side code.

However, carefully rereading your question, it looks like what you really want to see is the actual JSON text. Looking at @dystroy's answer above, I think that if you remove the dataType: 'json' from your $.ajax invokation, jQuery will treat the response as plain text instead of automatically converting it to an Object. In this case, you can see the text by passing it to the alert function.

4 Comments

@dystroy, in my experience, a generic object becomes [object Object] when cast to a string. Do you know of a way to change this behavior?
You're not really wrong. What I pointed is that alert doesn't "expect" especially "a string or number". It's more the user who expect something more readable than [object Object]. And there is nothing special with numbers.
@Daniel Allen Langdon this one works and I can see on alert window, is this a test code or can i also use in production to get jsonvalues to "x"
0

Try using

data = JSON.parse(data)

Then do whatever you want with the data.

Source: JSON.parse() (MDN)

3 Comments

I get SyntaxError: JSON.parse: unexpected character
because if you check my code, you see that it returns object but your function parses strings
Oh, my bad. Try using a console.dir() instead of console.log() or alert() to see the content of your object, at least you'll be able to see if it's you receive the right answer in your console. You might want to take a look at stackoverflow.com/questions/1637334/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.