Ajax Content Type Handling in jQuery

In order to do this, we’ll have to use the ajax method, because the success function sends back the XMLHttpRequest object that we’ll use to read the content type.

$.ajax({
  type: "POST",
  url: "/widgets", 
  data: widgetForm.serialize(), 
  success: function(response, status, xhr){ 
    var ct = xhr.getResponseHeader("content-type") || "";
    if (ct.indexOf('html') > -1) {
      widgetForm.replaceWith(response);
    }
    if (ct.indexOf('json') > -1) {
      // handle json here
    } 
  }
});

This is perfect for handling different mime types sent from the server! =)

Loading mentions Retweet
| Viewed
times
|
Favorited 0 times