Ajax-send a request to the server
To send requests to the server, we need to use the open () method and send () method.
The open () method requires three parameters. The first parameter defines the method (get or post) used to send the request ). The second parameter specifies the URL of the server script. The third parameter specifies that requests should be processed asynchronously.
The send () method can send requests to the server. If the HTML and ASP files are in the same directoryCodeYes:
XMLHTTP.Open
("Get", "Time. asp", true );
XMLHTTP.Send
(Null );
Now, we must decide when to execute Ajax functions. When you type some content in the User Name text box, the function will be executed "behind the scenes.
<HTML>
<Body>
<SCRIPT type = "text/JavaScript">
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)
;
}
</SCRIPT>
<Form name = "myform">
User: <input type = "text" name = "username" Onkeyup = "ajaxfunction ();"
/>
Time: <input type = "text" name = "time"/>
</Form>
</Body>
</Html>