Five steps for Ajax applications __ajax

Source: Internet
Author: User

Five Steps for AJAX applications

1. Create a XMLHttpRequest Object
var xmlhttp=createxmlhttprequest ();
function Createxmlhttprequest ()
{
var xmlHttp;
if (window.xmlhttprequest)
{
IE6 above version and other browsers built-in XMLHttpRequest objects
Xmlhttp=new XMLHttpRequest ();
}
else if (window.activexobject)
{
The IE6 and the following browsers can be created by ActiveXObject two controls, putting the new version in front
var activexname=["MSXML2. XMLHTTP "," Microsoft.XMLHTTP "];
for (var i=0; i<activexname.length; i++)
{
Try
{///Remove a control name for creation, create success to terminate the loop
Xmlhttp=new ActiveXObject (Activexname[i]);
Break
}
catch (e) {};
}
}
Confirm that the XMLHttpRequest object was created successfully
if (!xmlhttp)
{
Alert ("XMLHttpRequest object creation failed.) ");
Return
}
Else
{
alert (xmlHttp);
return xmlHttp;
}
}

2. Register callback function
Xmlhttp.onreadystatechange=callback;

3. Initialize the XMLHttpRequest object and set the connection information
The first parameter represents the way HTTP is requested, divided into get and post two, and the get-mode-requested parameter is in the URL
The second parameter represents the URL address of the request, which is resolved to an absolute address
The third parameter indicates whether the request is synchronous or asynchronous, and the default is sync True, and four, five parameters are usually not
Xmlhttp.open ("Get", url, True);

4. Sending data, starting and delivering on the server side
Synchronous mode: Send () This sentence will be sent back to the server side of the data after the execution, and then execute the next statement
Asynchronous mode: Send () This sentence will be executed immediately, followed by the next statement
To post, you need to set the HTTP request headers yourself
Xmlhttp.open ("POST", url,true);
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xmlhttp.send ("name=" +value);
Xmlhttp.send (NULL); This parameter is optional and can not be written at this time


5. Defines a callback function that is primarily responsible for receiving data from the server side and displaying it on the page
function callback ()
{
To determine whether an object's interaction state is complete
if (xmlhttp.readystate==4)
{
To determine if HTTP interaction is successful
if (xmlhttp.status==200)
{
Gets the data returned by the server side, in plain text data or in XML document format
var Responsetext=xmlhttp.responsetext;
Display response data on a page
Get document nodes in DOM or jquery
var Divnode=document.getelementbyid ("#divNode_ID");
Divnode.innerhtml=responsetext;
}
}

}

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.