浏览器控制台模拟接口请求

打开浏览器-->更多工具-->开发者工具

 

 

 控制台输入如下代码

fetch(new Request('url地址',{method:'POST'})).then((resp)=>{console.log(resp)})

注意:url地址是需要访问的接口路径,如果需要传递参数,就将请求参数添加到url后面即可(与get方式一样)

如果参数特别长,建议使用如下:

//方法一:
var url = "/dict/test";
var params = {advertiserUid: 1232131, advertiserWeiboNickname: "18"};
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));
 
//方法二:
var url = "/dict/test";
var params = "name=5&password=6";
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
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(params);

点击回车,查看结果

posted @ 2022-06-23 13:27  Jim-vue  阅读(1543)  评论(0编辑  收藏  举报