tags:

views:

56

answers:

3

How do I get at the raw JSON response from a jQuery $.getJSON() request?

I just want to print the raw response in an alert() dialogue in my browser?

+2  A: 

If you're using JSONP, this is fundamentally impossible.

If you're sending a normal request to your domain, replace getJSON with get.

SLaks
I'm not using JSONP. Tried your suggestion and I just get `[object Object]` displayed.
Kev
@Kev: Try adding `dataType: 'text'`.
SLaks
After an amount of "farting about", that does work. It does mean an extra step to then parse the string to an object, but then `$.dump()` or `JSON.stringify()` is an extra step the other way.
Kev
A: 

http://api.jquery.com/jQuery.getJSON/

I prefer to use the full .ajax methods so I don't have to remember the various signatures of the abstractions.

The docs tell you this is equivalent to getJSON:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

where you could have:

function callback(data) {
    alert(data);
}

I strongly recommend that you use Firefox and Firebug with console.log(); for this kind of thing. Alerts drive you nuts after a while.

Edit

Based on the other responses, I may be misunderstanding your question! You could always use Fiddler to see the raw response.

ScottE
It's the raw json string that came across the wire I want to print. It was just so I could do a quick hacky debug thing to check an array value. But thanks all the same.
Kev
Firebug is the way to go then. Just change to function callback(data) { console.log(data);} to see the whole object returned.
ScottE
`console.log()` still dumps the data as if it's an object, not really what I'm after. I don't get to see the raw JSON (curly braces, square brackets, field names, warts and all). Fiddler does work but I wanted to embed this in a 'secret' textbox ultimately.
Kev
A: 

I ended up using the JSON.stringify() feature in http://www.json.org/json2.js.

Not ideal because it's another javascript file being loaded, but it serves my purpose.

Kev
Note that this will not necessarily return the actual JSON you received. (The formatting may be different)
SLaks
@slaks - yep but it momentarily served a purpose.
Kev