Native AJAX Request data
Btn.addeventlistener (' click ',function(){ if(window. XMLHttpRequest) {varXHR =Newwindow. XMLHttpRequest (); }Else{ //compatible with IE browser varXHR =NewActiveXObject (' Microsoft.XMLHTTP '); } //Request Method Get/post //Request URL //ture asynchronous request; false synchronous requestXhr.open (' Get ', '/ajax/getdata ',true); //changes to XHR binding event listening state (status code see below)Xhr.onreadystatechange =function(){ if(Xhr.readystate = = 4 && xhr.status = 200) {console.log (xhr.responsetext); } } //Send Request Data//get method The send parameter is empty or nullXhr.send (); });
Ajax encapsulation implemented by JQuery
// jQuery $ ('. Btn '). On (' click ',function() { $.ajax ({ URL:'/ajax/getdata ', type:') Get ', dataType:' json ', // successfully execute function success:functions (response,status) { console.log (responsetext); } ); });
Xhr.readystate Status Code
0:unsend//Current request not yet sent
1:opened//url Address is open
2:headers_received//Response header information has been received
3:loading//Primary return data is being prepared for processing on the server
The contents of the 4:done//response body have been successfully returned to the client
Xhr.status Server Status Code
200 and starting with 2: success (the body of the response has successfully returned to the client)
301: Permanent Redirect/Transfer
302: Temporary REDIRECT/transfer
304: This time gets the content is read the cache
400: Incorrect parameter for server with client
401: No Access
404: Access address does not exist
500: Unknown server error
501: Server overload
The data is not complete for reference only----------
Ajax request data and status codes in native and jquery packages