JavaScript implements native Ajax

Source: Internet
Author: User

Since JavaScript has a variety of frameworks, such as jquery, using Ajax has become quite simple. But sometimes in order to pursue brevity, there may be no need to load jquery's huge JS plugin in the project. But what about the ability to use AJAX? Here's a way to share JavaScript for native Ajax.

You must create an XMLHttpRequest object before you implement Ajax. If you do not support the browser that created the object, you need to create a activexobject, as follows:

varfunctionifnew ActiveXObject ("Microsoft.XMLHTTP"  Elseif  (window. XMLHttpRequest) {xmlHttp=new  XMLHttpRequest ();}

(1) The simplest AJAX GET request is implemented using the XMLHTTP created above:

function//createxmlhttprequest (); Xmlhttp.open ("GET", URL); Xmlhttp.send (nullfunctionif (xmlhttp.readystate = = 4) & & (Xmlhttp.status = =)) {alert (' success 'else  {alert (' fail ')

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

function//createxmlhttprequest (); Xmlhttp.open ("POST", URL); Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded"function   if ((xmlhttp.readystate = = 4) && (xmlhttp.status = =)) {alert (' success  ' Else {alert (' fail '

JavaScript implements native Ajax

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.