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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.