Create XMLHttpRequest in basic JavaScript to send Ajax requests

Source: Internet
Author: User

Basic Ajax writing

<Script language = "JavaScript" type = "text/JavaScript">

// Create XMLHttpRequest
Function createxmlgttprequest (){
If (window. activexobject) {// IE browser
Return new activexobject ("Microsoft. XMLHTTP ");
} Else if (window. XMLHttpRequest) {// non-IE browser
Return new XMLHttpRequest ();
}
}

// send the request
var XMLHttpRequest; // declare the XMLHTTPRequest object
function tosendrequest () {
XMLHttpRequest = createxmlgttprequest ();
XMLHttpRequest. onreadystatechange = callbackfun; // sets the callback function. When the status of the XMLHTTPRequest object changes, call the callbackfun function.
// initializes the XMLHttpRequest component; the third parameter indicates synchronous (false) or asynchronous (true ), asynchronous means that after calling the send method to send a request, you do not have to wait for the request response to continue executing the following JavaScript Code .
XMLHttpRequest. Open ("get", "Ajax. ashx? Name = name1 & pass = pass1 ", true);
XMLHttpRequest. Send (null); // send a request
}

// callback function
function callbackfun () {
// readystate is in 5 states, 0 is not initialized, 1 is initialized, 2 is sent, 3. Receive the result. 4. The result is received.
// status server response status code.
If (XMLHttpRequest. readystate = 4 & XMLHttpRequest. status = 200) {
alert (XMLHttpRequest. responsetext);
}else {
// exception
}< BR >}< br>

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.