1. Home
  2. Computing & Technology
  3. JavaScript

Introduction to Ajax

7. Response Type

clr gif

If you are the one creating the entire AJAX application then you ought to know exactly what format you expect to get your response in. We can of course confirm this by checking the content type defined in the response headers.

Normally the response that we are expecting to get back will be either an XML or a plain text format so we can test which of these that we have using the following code:

ajaxObj.processRequest = function() {
if (this.readyState == 4) {
if (this.status != 200) {
alert('Error : Status '+this.status+' returned.');
} else {
var cType =
this.getResponseHeader("Content-Type");
if (cType == 'text/xml') {
// XML response
} else if (cType == 'text/plain') {
// plain text response
} else {
alert('unknown content type');
}

}
}
}

Explore JavaScript

More from About.com

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Ajax
  5. Learn AJAX
  6. Response Type

©2008 About.com, a part of The New York Times Company.

All rights reserved.