How to get original JSON response from jQuery $ .getJSON () request?

How to get original JSON response from jQuery $.getJSON() request?

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

+7
json jquery
source share
5 answers

If you use JSONP, this is fundamentally impossible.

If you are sending a regular request to your domain, replace getJSON with get .

+3
source share

In jQuery 1.5, the jqXHR object is passed as the third argument to the callback method. The jqXHR object has a reponseText property that contains a raw JSON string.

 function callback(data, status, jqXHR) { alert(jqXHR.responseText); // or console.log(jqXHR.responseText); } 
+14
source share

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

I prefer to use the full .ajax methods, so I don’t need to remember the various signatures of abstractions.

The docs tell you this is equivalent to getJSON:

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

where could you:

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

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

Edit

Based on other answers, I may not understand your question! You can always use Fiddler to see the original answer.

0
source share

I ended up using the JSON.stringify() function in https://github.com/douglascrockford/JSON-js/blob/master/json2.js .

Not ideal because another javascript file is loading, but it serves my purpose.

0
source share

There are several different options for getting the actual answer: whether it was processed by JSON or the error created by the browser / server that the getJSON method tried to parse, but could not.

Note: when using the ".always" method, the parameters change depending on failure or success.

 $.getJSON(getData.php, function(data,textStatus,jqXHR) { console.log(jqXHR.responseText); //As of 1.5 we have these methods: }).always( function(jqXHR, textStatus, errorThrown) { //on failure console.log(jqXHR.responseText); }).always( function(data, textStatus, jqXHR) { //on success console.log(jqXHR.responseText); }).fail( function(jqXHR, textStatus, errorThrown) { console.log(jqXHR.responseText); //As of 1.8 we have this method also: }).then( function(data, textStatus, jqXHR) { console.log(jqXHR.responseText);}, function(jqXHR, textStatus, errorThrown) { console.log(jqXHR.responseText); }); 

If you want more information:
http://api.jquery.com/jQuery.ajax/#jqXHR or
http://api.jquery.com/category/deferred-object/

0
source share

All Articles