Handling the Server Response in AJAX

By: Ram Baskar Emailed: 1643 times Printed: 2110 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Remember that when you send the request, you provide the name of a JavaScript function that is designed to handle the response. eg:

http_request.onreadystatechange = nameOfTheFunction;

Let's see what this function should do. First, the function needs to check for the state of the request. If the state has the value of 4, that means that the full server response is received and it's OK for you to continue processing it.

if (http_request.readyState == 4) {
    // everything is good, the response is received
} else {
    // still not ready
}

The full list of the readyState values is as follows:

  • 0 (uninitialized)
  • 1 (loading)
  • 2 (loaded)
  • 3 (interactive)
  • 4 (complete)

The next thing to check is the status code of the HTTP server response. For our purposes we are only interested in 200 OK response.

if (http_request.status == 200) {
    // perfect!
} else {
    // there was a problem with the request,
    // for example the response may be a 404 (Not Found)
    // or 500 (Internal Server Error) response codes
}

Now after you've checked the state of the request and the HTTP status code of the response, it's up to you to do whatever you want with the data the server has sent to you. You have two options to access that data:

  • http_request.responseText – will return the server response as a string of text
  • http_request.responseXML – will return the response as an XMLDocument object you can traverse using the JavaScript DOM functions

Ajax Home | All Ajax Tutorials | Latest Ajax Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ram Baskar
Hello World sample program in C#
Comments in C#
Preprocessor directives in C#
Boxing and unboxing in C#
Categories of datatypes in C#
Major features of C#
Which version of C# to use?
History of C#?
Why it was named C#?
Design Goals of C#
What is C#?
A complete sample program in AJAX
Handling the Server Response in AJAX
How to Make an HTTP Request in AJAX

More Tutorials in Ajax
How to Make an HTTP Request in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
The History of AJAX
To use or not to use AJAX

More Latest News
Most Viewed Articles (in Ajax )
To use or not to use AJAX
Creating JSON objects using Java in AJAX
How to Make an HTTP Request in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
The History of AJAX
XML vs JSON in AJAX
Most Emailed Articles (in Ajax)
A complete sample program in AJAX
The History of AJAX
To use or not to use AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
Handling the Server Response in AJAX
How to Make an HTTP Request in AJAX