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.)
Well, if you are using jQuery and you specify the
dataTypeproperty of the$.ajax()call tojsonthen jQuery will try to parse the JSON, and if it isn’t JSON should call theerror()callback.EDIT
For anyone not using jQuery that lands here, the basic idea is to try and parse it as json and catch the error: