(1) establish ajax object
var xhr = new XMLHttpRequest();
(2) Open request
// The request method is customized , The third parameter is usually set to true, Asynchronous request xhr.open('GET', url, true);
(3) Send request
// Optional , Set request header , According to the need ,post Write the request
//xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.
send( Data to send );
(4) Receive response
// Server response status (readyState) Changes are executed xhr.onreadystatechange = function(){ //0
( uninitialized ) Object has been created , But it hasn't been called yet open() method //1 ( start-up ) Already called open() method , But it has not been called send() Method to send the request //2
( send out )send() Method called , The request has been sent , But no response has been received //3 ( receive ) Partial response data has been received //4
( complete ) All data has been received , And it can be used in the client // Server response status (readyState) And responsive HTTP state (status) Success is to be satisfied at the same time if
(xhr.readyState==4 && xhr.status==200){ // adopt xhr.responseText, Get the content returned by the server console.
log(xhr.responseText) } }