AJAX – Request a Server

To send off a request to the server, we use the open() method and the send() method.

The open() method takes three arguments. The first argument defines which method to use when sending the request (GET or POST). The second argument specifies the URL of the server-side script. The third argument specifies that the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that the HTML and ASP file are in the same directory, the code would be:
xmlHttp.open(“GET”,”time.asp”,true);
xmlHttp.send(null);
Now we must decide when the AJAX function should be executed. We will let the function run “behind the scenes” when the user types something in the username text field:

Name:
Time:

function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e)
{
alert(“Your browser does not support AJAX!”);
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open(“GET”,”time.asp”,true);
xmlHttp.send(null);
}

Name:
Time:
3 Responses to AJAX – Request a Server
  1. Cnizz Web Development
    December 8, 2008 | 5:22 pm

    Don’t you feel this method of performing an XHR request is a bit dated now with all the frameworks such as mootools and jquery that handle this sort of thing for you? Don’t get me wrong, this is how I originally learned how to perform XHR requests, but I feel frameworks are the way to go now.

  2. Pages tagged "ajax"
    December 8, 2008 | 4:47 am

    […] bookmarks tagged ajax AJAX – Request a Server : Ajax Blog Ajax Tutorial … saved by 3 others     ototochan88 bookmarked on 12/08/08 | […]