Ajax-request Server

Source: Internet
Author: User
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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.