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
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

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

All rights reserved.