responseType in $.ajax

Luaox picture Luaox · Jan 19, 2013 · Viewed 12.7k times · Source
$.ajax({
   type: "POST",
   url: "bla",
   xhrFields: { responseType: "document" },
   data: {},
   success: function(arg,arg2,request){
      console.log(request.responseXML)
   }
})

Why is it printing 'undefined'? How would I fix this?

Answer

hohner picture hohner · Jan 19, 2013

Are you expecting a JSON return? What happens when you try:

$.ajax({
  type: "POST",
  url: "bla",
  dataType: 'xml',
}).done(function (response) {
   console.log(response);
});

If you look at jQuery's documentation, they outline how:

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method.

The response variable therefore contains what you need. To see its structure, do a console.log() and go to the 'Console' tab in your Developer Tools (Chrome) or Firebug (Firefox).