Using JavaScript to implement AJAX instance code _javascript tips

Source: Internet
Author: User

AJAX = asynchronous JavaScript and XML.

AJAX is a technique for creating fast Dynamic Web pages.

AJAX allows Web pages to be updated asynchronously by exchanging small amounts of data in the background with the server. This means it is possible to update part of a Web page without overloading the entire page.

You must create a XMLHttpRequest object before you implement Ajax. If you do not support the browser that created the object, you need to create activexobject. The following are the specific methods:

var xmlHttp; 
function Createxmlhttprequest ()
{ 
if (window). ActiveXObject) { 
xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP"); 
} 
else if 
(window. XMLHttpRequest)
{ 
xmlhttp=new xmlhttprequest (); 
} 

(1) Use the XMLHTTP created above to implement the simplest Ajax GET request:

function doget (URL)
{ 
//Note the best use of encodeURI when passing parameter values to prevent garbled 
createxmlhttprequest (); 
Xmlhttp.open ("get", url); 
Xmlhttp.send (null); 
Xmlhttp.onreadystatechange = function ()
{ 
if (xmlhttp.readystate = 4) && (Xmlhttp.status = 200)) { C22/>alert (' success '); 
} 
else 
{ 
alert (' fail '); 
} 
} 

(2) Use the XMLHTTP created above to implement the simplest Ajax POST request:

function DoPost (url,data)
{ 
//Note the best use of encodeURI when passing parameter values to prevent garbled 
createxmlhttprequest (); 
Xmlhttp.open ("POST", url); 
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"); 
Xmlhttp.send (data); 
Xmlhttp.onreadystatechange = function () 
{ 
if (xmlhttp.readystate = 4) && (Xmlhttp.status = 200)) c11/>{ 
alert (' success '); 
}
else
{ 
alert (' fail '); 
} 
} 

The above content is small to introduce the JavaScript AJAX implementation of the example code, I hope to help you, in the use of the process found any questions welcome to my message, small series will promptly reply to everyone. In this small series thank you very much for the cloud Habitat Community website support, I believe we will do better!

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.