1 //A. Native JS implements AJAX requests2 // 1.get Request3 varXml=NULL;4 if(XMLHttpRequest) {5Xml=NewXMLHttpRequest;6}Else{7Xml=NewActiveXObject (' Microsoft.XMLHTTP ')8 }9Xml.open (' GET ', url,true); Ten xml.send (); OneXml.onreadystatechange=function(){ A if(xml.readystate==4&&xml.status==200){ - //Request succeeded -}Else{ the //request failed - } - } - // 2.post Request + varXml=NULL; - varData={a:1,b:2}; + if(XMLHttpRequest) { AXml=NewXMLHttpRequest; at}Else{ -Xml=NewActiveXObject (' Microsoft.XMLHTTP ') - } -Xml.open (' POST ', url,true); - xml.send (data); -Xml.onreadystatechange=function(){ in if(xml.readystate==4&&xml.status==200){ - //Request succeeded to}Else{ + //request failed - } the } * //Two. JQ implements AJAX requests $ //1.get RequestPanax Notoginseng $.ajax ({ -Type: "Get", theURL: "", +Asynctrue, ASuccessfunction(){ the //Request succeeded + }, -Errorfunction(){ $ //request failed $ } - }); - //2.post Request the $.ajax ({ -Type: "POST",WuyiURL: "", theData:{a:1,b:2}, -Asynctrue, WuSuccessfunction(){ - //Request succeeded About }, $Errorfunction(){ - //request failed - } - }); A //Three. Axios Request: + //first install the Axios, the //method One: npm install Axios - //method Two: CDN introduces <script src= "Https://unpkg.com/axios/dist/axios.min.js" ></script> $ //1.get Request (no parameters) theAxios.get (' http://www.xxx ') the. Then (function(response) { the //Request succeeded the}).Catch(function(Erroe) { - //request failed in }); the //2.get request (with parameters) theAxios.get (' http://www.xxx?a=1&b=2 ') About. Then (function(response) { the //Request succeeded the}).Catch(function(Erroe) { the //request failed + }); - //3.post Request: the //QS must be introduced to the data for stringify installation Axios when the QS has been installed, no need to install, the introduction can be used. Bayi //import qs from ' QS ' theLet Data=qs.stringify ({a:1,b:2}); theAxios.post (' http://www.xxx ', data) -. Then (function(response) { - //Request succeeded the}).Catch(function(Error) { the //request failed the }) the //4. Simultaneous sending of multiple requests - functionAxiosone () { the returnAxios.get (' Http://www.url.one ') the }; the functionAxiostwo () {94 returnAxios.get (' Http://www.url.two ') the }; the Axios.all ([Axiosone (), Axiostwo ()]) the. Then (Axios.spread (function(Acct, perms) {98Console.log (ACCT);//The result of a request; AboutConsole.log (perms);//result of request two -}))
1, Ajax, Axios, JSONP Summary