In-depth understanding of Ajax get and post requests and ajaxget
1. get request
Function () {// request time to the server // 1. create an asynchronous object (small browser) var xhr = new XMLHttpRequest (); // 2. set the parameter. "true" indicates that the asynchronous mode xhr is used. open ("get", "GetTime. ashx? Name = Mr jing ", true); // 3. make the get request not to obtain the cached data from the browser xhr. setRequestHeader ("If-Modified-Since", "0"); // 3. set the callback function xhr. onreadystatechange = function () {// 3.1 if (xhr. readyState = 4 & xhr. status = 200) {// 3.2 obtain the corresponding style content var res = xhr. responseText; alert (res) ;}; // 4. send asynchronous request xhr. send (null );}
2. post request
Function () {// request time to the server // 1. create an asynchronous object (small browser) var xhr = new XMLHttpRequest (); // 2. set the xhr parameter. open ("post", "GetTime. ashx ", true); // 3. set the encoding format of the Request Message style (set to the default form encoding format) xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); // 4. set the callback function xhr. onreadystatechange = function () {// 3.1 if (xhr. readyState = 4 & xhr. status = 200) {// 3.2 obtain the corresponding style content var res = xhr. responseText; alert (res) ;}; // 5. send asynchronous request "name = Mr jing" // 5.1 format: directly splice string key = value & key1 = value2 xhr. send ("name = Mr Jing & age = 18 ");};
The above in-depth understanding of Ajax get and post requests is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support for the help house.