pu369com

chrome控制台发送post请求

chrome控制台发送post请求
chrome, 按F12,打开控制台, 执行下面脚本

代码

var url = "https://www.test.com/Admin/testpost;
var params = {id:"1",name:"q"};
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function (e) {
  if (xhr.readyState === 4) {
    if (xhr.status === 200) {
      console.log(xhr.responseText);
    } else {
      console.error(xhr.statusText);
    }
  }
};
xhr.onerror = function (e) {
  console.error(xhr.statusText);
};
xhr.send(JSON.stringify(params));

补充:

也可直接在chrome页面按下Ctrl+shif+J ,在网络中刷新页面,在相应的请求上,复制 fetch格式,即可用来在console中发送POST请求

对于POST请求返回的是readablestream的,可用以下代码显示在console中:

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(function(response) {
    // The response is a Response instance.
    // You parse the data into a useable format using `.json()`
    return response.json();
  }).then(function(data) {
    // `data` is the parsed version of the JSON returned from the above endpoint.
    console.log(data);  // { "userId": 1, "id": 1, "title": "...", "body": "..." }
  });

 

参考:

https://stackoverflow.com/questions/40385133/retrieve-data-from-a-readablestream-object

https://www.cnblogs.com/barrysgy/p/15696559.html

posted on 2022-08-26 15:22  pu369com  阅读(4502)  评论(0编辑  收藏  举报

导航