Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to tell whether AJAX response is JSON

I've got an AJAX request that expects JSON in response.

But there's a possibility that what gets returns may not be JSON, but rather an HTML error page (unfortunately, with response type 200).

How can I tell whether the response is JSON or not?

(I'm using jQuery, if that helps. But I can't use any plugins.)

like image 279
jawns317 Avatar asked Sep 02 '11 19:09

jawns317


People also ask

How do I know if AJAX response is JSON?

If the response is JSON, a properly behaving application would set the Content-Type to application/json. So all you have to do, if the server is well-behaving, is to test if the Content-Type header in the response starts with application/json. By chance, jQuery already does this by itself: $.

How do I know if a response is JSON?

JSON Check The fetch . then() callback is passed the HTTP response object when the request is completed, the function checks if the response type is JSON before parsing the response body with the response. json() method, because calling response. json() will cause an error if the response doesn't contain JSON data.

Does AJAX return JSON?

In this tutorial, I showed how you can return the JSON response and handle it in jQuery AJAX. You can convert the PHP array in JSON format with json_encode() function and return as a response. Set dataType: 'JSON' when send AJAX request.

What is the difference between AJAX and JSON?

AJAX is utilizing for planning the internet page appropriately, particularly where the page needs a few server-side information without reviving the same. JSON isn't utilizing for only planning the net page. In fact, JSON some of the time not at all utilized for the net application.


2 Answers

Well, if you are using jQuery and you specify the dataType property of the $.ajax() call to json then jQuery will try to parse the JSON, and if it isn't JSON should call the error() callback.

$.ajax({     url: '/my/script.ext',     dataType: 'json',     success: function(data, textStatus, jqXHR) { /*YAYE!!*/ },     error: function(jqXHR, textStatus, errorThrown) { /*AWWW... JSON parse error*/ } }); 

EDIT

For anyone not using jQuery that lands here, the basic idea is to try and parse it as json and catch the error:

var data = 'some_data';  try {     data = JSON.parse(data); } catch(e) {     //JSON parse error, this is not json (or JSON isn't in your browser) }  //act here on the the parsed object in `data` (so it was json). 
like image 178
Chad Avatar answered Oct 03 '22 04:10

Chad


jQuery auto-detects the dataType:

If the response is JSON, a properly behaving application would set the Content-Type to application/json.

So all you have to do, if the server is well-behaving, is to test if the Content-Type header in the response starts with application/json.

By chance, jQuery already does this by itself:

$.get('/foo', function(data, status, xhr, dataType) {     if ('json' === dataType) {         // Yay that's JSON !         // Yay jQuery has already parsed `data`      } }); 

jQuery detects the dataType and passes it as 4th parameter of the callback function. If the dataType is JSON, it parsed the JSON string and parses the resulting value as first parameter of the callback.

like image 32
Arnaud Le Blanc Avatar answered Oct 03 '22 04:10

Arnaud Le Blanc