There are four steps to a native Ajax request
- Create an object
- The connection
- send
- Through to
var xhr = new XMLHttpRequest();
xhr.open('GET'.'url'.true) // True,false indicates whether asynchronous, usually true
xhr.send()
xhr.onreadystatechange = function (){
console.log(xhr.readyStates)//2,3, and 4 indicate that the request has been received
if(xhr.readyState==4&&xhr.status==200) {Xhr. status Indicates the status code of the HTTP request result
console.log(xhr.responseText)
}
}
Copy the code
Xhr. send(“params=value&lparamter=value2”) xhr.send(“params=value&lparamter=value2”)
Xhr.setrequestheader (” Content-type “,”application/x-www-form-urlencoded”); xhr.setrequestheader (” content-type “,”application/x-www-form-urlencoded”)
Roughly:
One event, two methods, three statesCopy the code