Core Ajax technology code sharing

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script>
Var xhr = '';
Function Ajax ()
{
If (window. XMLHttpRequest)
{
Var xhr = new XMLHttpRequest; // modern Browser
} Else
{
Var xhr = new ActiveXObject ('Microsoft. xmlhttp'); // IE
}
}
Ajax (); // 1. Obtain the Ajax object

Xhr. onreadystatechange = function ()
{
If (xhr. readyState = 4 & xhr. status = 200)
{
Var data = xhr. responseText; // 2. determine the status, receive data (in string format), and perform subsequent operations
}
}

// Xhr. open ('request method ', 'request addresses', TRUE); // 3. asynchronous transmission connection
// Xhr. send ([data]); // 4. send the request

Xhr. open ('get', 'demo. php? Name = weichen & sex = 0', TRUE );
Xhr. send ();

/*
Xhr. open ('post', 'demo. php', TRUE );
Xhr. setResponseHeader ('content-type', 'application/x-www-form-urlencoded ');
Xhr. send ('name = weichen & sex = 0 ');
*/

Member attributes and member methods in XHR objects

Copy codeThe Code is as follows:
{

Member attributes:
HTTP status Code (for example, 200 or 404) returned by the status Server)
HTTP status code (OK or not found) returned by the statusText server in string format)
Status code requested by readyState browser (0 to 4)
ResponseText Server Response string format data result
ResponseXML Server Response XML format data result
Onreadystatechange Ajax request event (the event processing function called when readystate changes)
The Mozilla event handler called when an error occurs in the onerror request process
The mozilla event handler called after the onprogress content is loaded
The mozilla event processing function called after the onload document is loaded
Member method:
SetRequestHeader (); set the header information of the current request (add a key/value pair for the message header to be sent to the server)
Open (); create a new request connection (use GET, POST, URL, and other initialization XHR objects)
Send (); send a request (data that may need to be sent)
GetResponseHeader ("server"); returns the value of the specified HTTP header (such as server or last-modified)
GetAllResponseHeaser (); returns all HTTP header strings separated by line breaks
Absort (); terminate the request

}

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.