I need to make an Ajax request, but its response may vary and it’s decided on the server side.
Is there any way to know what type the response is?
It may look similar to:
$.post(url, pars, function (response, type) {
if (type=='json') ...
if (type=='html') ...
});
There’s no built-in way to do this, it’s determined and tossed away by
jQuery.httpData(note: it will bejquery.ajax.httpDatain 1.4.3).Though you can take a look at the httpData source and run the same functions yourself, that’s a bit wasteful, since jQuery’s doing it already. I
If your choices are only
jsonorhtml, you could checktypeof response, it should be"string"for HTML, otherwise you have JSON, which you could also check and be sure about as well, for example:type && type.propertyAlwaysThere.