Getting started with Ajax

Source: Internet
Author: User

<SCRIPT type = "text/JavaScript">

Function btnonchink (){

VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP"); // create an XMLHTTP object, which is equivalent to WebClient

If (XMLHTTP ){

Alert ("An error occurred while creating the XMLHTTP object! ");

Return false;

}

XMLHTTP. Open ("Post", "getdata1.ashx? TS = "+ new data (), false); // prepare to send a POST request to getdata1.ashx on the server

TS = "+ new data () is used to transmit different parameter values to the server each time, so that the browser will not directly read the post from the cache and will not cache it. Get may be cached. If the parameter value is Chinese, encodeuri ("China") is required ")

 

// XMLHTTP is not a synchronous request by default (recommended), that is, the open method is not like webclientdonwloadstring

In this way, the data returned by the server is obtained and returned asynchronously. Therefore, you need to listen to the onreadystatechange event.

XMLHTTP. onreadystatechange = function (){

If (XMLHTTP. readstate = 4) {// The server completes the response

If (XMLHTTP. Status = 200) {// If the status code is 200, data is returned successfully.

Alert (XMLHTTP. responsetext); here is the text returned by the server

Document. getelementbyid ("textbox1"). value = XMLHTTP. responsetext; // return the text to the text box.

}

Else {

Alert ("error returned by Ajax server! ");

}

}

}

XMLHTTP. Send (); // The request starts to be sent.

}

</SCRIPT>

 

 

 

The following are jquery Ajax versions:

<SCRIPT type = "text/JavaScript">

Function button1_onclick (){

VaR txtbox1 = $ ("# txtbox1"). Val ();

VaR txtbox2 = $ ("# txtbox2"). Val ();

$. Post ("getdata. ashx", {"ID": txtbox1, "name": txtbox2}, function (data, textstatus ){

If (textstatus = "success "){

Alert ("successful ");

}

Else {

Alert ("ajax error! ");

}

})

}

</SCRIPT>

 

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.