<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>