In my $.ajaxSucess() function I need to find out if the response is json. Currently I am doing this:
$('body').ajaxSuccess(function(evt, xhr, settings) {
var contType = xhr.getAllResponseHeaders().match(/Content-Type: *([^)]+);/);
if(contType && contType.length == 2 && contType[1].toLowerCase() == 'application/json'){
...
Is there a better way?
Assuming that you are expecting json, I’d simply try and parse it like json and catch any errors. Also see jQuery.parseJSON.
If you are expecting one of a number of different response types (i.e. it might be json or it might just be text, etc) then you might need to get more complicated. I’d use xhr.getResponseHeader(“content-type”). See this blog post for some great detail on handling content types.