Chrome浏览器如何模拟发起post请求。

       众所周知,浏览器发起get请求,直接输入url即可,那么chrome浏览器如

何模拟发起post请求(不使用插件的情况)。

步骤:

1.F12打开开发模式,点击Console。

2.复制以下代码,使用XMLHttPRequest(用于在后台与服务器交换数据)。

1. var url = "http://****";

2. var params = {loginName: 'admin', password: "XXXX"};

3. var xhr = new XMLHttpRequest();

4. xhr.open("POST", url, true);

5. xhr.setRequestHeader("Content-Type", "application/json");

6. xhr.onload = function (e) {

7. if (xhr.readyState === 4) {

8. if (xhr.status === 200) {

9. console.log(xhr.responseText);

10. } else {

11. console.error(xhr.statusText);

12. }

13. }

14. };

15. xhr.onerror = function (e) {

16. console.error(xhr.statusText);

17. };

18. xhr.send(JSON.stringify(params));

 

posted @ 2021-08-23 17:28  划边逅  阅读(6885)  评论(0编辑  收藏  举报