JavaScript原生GET,POST请求
JavaScript原生GET,POST请求
JavaScript原生GET,POST请求
1 2 3 4 5
| <p><button onclick="fooGet()">GET 请求 大飞</button> </p> <p>22222</p> <p>22222</p> <p>22222</p> <button onclick="fooPost()">POST 请求 DaFei</button>
|
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| function fooGet() { let url = "http://www.dafei.com/api/testGet.php?name=dafei"
let httpRequest = new XMLHttpRequest();
httpRequest.open('GET', url, true);
httpRequest.send();
httpRequest.onreadystatechange = function () { if (httpRequest.readyState === 4 && httpRequest.status === 200) { alert("get接口请求成功 ok") var json = httpRequest.responseText; console.log(json); } }; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function fooPost() { let url = "http://www.dafei.com/api/testPost.php"
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.setRequestHeader("Content-type", "application/json"); const obj = { foo: "123", bar: "456", name: "dafei" } httpRequest.send(JSON.stringify(obj));
httpRequest.onreadystatechange = function () { if (httpRequest.readyState === 4 && httpRequest.status === 200) { alert("post接口请求成功 ok") var json = httpRequest.responseText; console.log(json); } }; }
|