1.get request
function () { //Request time from the server//1. Create an asynchronous object (small browser) var xhr = new XMLHttpRequest(); //2. Set parameters, true means using the asynchronous mode xhr.open("get", "GetTime.ashx?name= Mrjing", true); //3. Make the get request not to obtain cached data from the browser xhr.setRequestHeader("If-Modified-Since","0"); //3. Set the callback function xhr.onreadystatechange = function () { //3.1 When the response message is completely received and the response status code is 200 if (xhr.readyState == 4 && xhr.status == 200) { //3.2 Get the corresponding message content var res = xhr.responseText; alert(res); } }; //4. Send asynchronous request xhr.send(null);}2.post request
function () { //Request time from the server//1. Create an asynchronous object (small browser) var xhr = new XMLHttpRequest(); //2. Set parameters xhr.open("post", "GetTime.ashx", true); //3. Set the encoding format of the request message body (set as the form default encoding format) xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //4. Set the callback function xhr.onreadystatechange = function () { //3.1 When the response message is completely received and the response status code is 200 if (xhr.readyState == 4 && xhr.status == 200) { //3.2 Get the corresponding message content var res = xhr.responseText; alert(res); } }; //5. Send asynchronous request "name=Mrjing" //5.1 Format: Direct splicing of string key=value&key1=value2 xhr.send("name=Mrjing&age=18"); };The above article in-depth understanding of Ajax's get and post request is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.