Five Steps for AJAX applications

Source: Internet
Author: User

1. Establish XMLHttpRequest objects

if (window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();

if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype ("Text/xml");
}
}else if (window. ActiveXObject) {
var activename =["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (var i=0; i<activename.length; i++) {
try{
XmlHttp = new ActiveXObject (Activename[i]);
Break
}catch (e) {
}
}

}
if (!xmlhttp) {
Alert ("Create XMLHttpRequest object Failed");
}else{

}

2. Set callback function

Xmlhttp.onreadystatechange= callback;

function callback () {}

3. Use the Open method to establish a connection with the server Xmlhttp.open ("Get", "ajax?name=" + name,true)

This step is to set the request for HTTP (post/get), and if it is post, pay attention to setting the request header information Xmlhttp.setrequestheader ("Content-type", "application/ X-www-form-urlencoded ")

4. Send data to server side

Xmlhttp.send (NULL);

If the Post method is not empty

5. Handling in a callback function for different response states

if (xmlhttp.readystate = = 4) {
Determine if the interaction is successful
if (Xmlhttp.status = = 200) {
Get data returned by the server
Get Plain Text data
var responsetext =xmlhttp.responsetext;
document.getElementById ("Info"). InnerHTML = ResponseText;
}
}


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.