I have experienced it by far...

Monday, March 23, 2009


Recently, I worked on AJAX for the first time to create a small application for the web-based product I am working on. I have faced few issues and resolved them. This blog is about some important points about AJAX which one should take care.

For XML Response:
  • Set the content type to "text/xml". Eg. response.setContentType("text/xml");
  • Before creating the XML content always clear the buffer. Calling clearBuffer() of JspWriter is better than calling just clear() method as the earlier won't throw any exception. Eg. out.clearBuffer();
  • While creating the XML content there has to be a root element. Eg.
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<name>Sujoy C</name>
<name>Tanmoy C</name>

To Post Data:
  • Set the content type of request header to "application/x-www-form-urlencoded"
  • Create the post parameters and set the content length. eg. xmlReqObj.setRequestHeader("Content-length", params.length);
  • Code example:
var xmlReqObj = new XMLHttpRequest();


{"POST", URL, true);

xmlReqObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlReqObj.setRequestHeader("Content-length", params.length);

xmlReqObj.setRequestHeader("Connection", "close");


} catch(err)


I shall try the following things and keep posted:
  1. How can I pass argument to the function onreadystatechange?
  2. The status attribute most of the time throws error. Why does this happen and how to resolve this?