Code for sending Ajax requests using pure JavaScript code
Source: Internet
Author: User
<Script language = "JavaScript" type = "text/JavaScript">
Function btnclick (){
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
If (! XMLHTTP ){
Alert ("An error occurred while creating the XMLHTTP object! ");
Return false;
}
XMLHTTP. Open ("Post", "getdata1.ashx? TS = "+ new date (), false); // prepare to send a POST request to the getdata1.ashx Server
// XMLHTTP is not a synchronous request by default (also recommended), that is, the open method does not return the data returned by the server as the downloadstring of WebClient. It is asynchronous because the onreadystatechange event needs to be monitored.
XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4) {// server complete
If (XMLHTTP. State = 200) {// success if the status code is 200
Alert (XMLHTTP. responsetext); // responsetext is the text returned by the server. The returned content is displayed.
}
Else {
Alert ("ajax server return error ");
}
}
}
XMLHTTP. Send (); // The request starts to be sent.
}
</SCRIPT>
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