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