当前位置:   article > 正文

axios的get和post请求传headers、query、body等参数_axios.get header

axios.get header

axios的get和post请求传headers、query、body等参数

一、get传headers和query

// 方法一:get请求传query可以拼接在url后面,也可以放在params中
const age = 15;
const result = await axios.get(`/a?age=${age}&type=1`,{
  headers: {
    "Content-Type": testApiData.value.extContentType,
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
// 方法二:get请求传query也可以放在params中
const query = {
  age: 15,
  type: 1,
};
const result = await axios.get("/a", {
  params: query,
  headers: {
    "Content-Type": testApiData.value.extContentType,
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.post请求传query、body、headers

// 方法一
const result = await axios.post("/a", body, {
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
  params: query
});
  • 1
  • 2
  • 3
  • 4
  • 5
// 方法二
const query = {
  age: 15,
  type: 1,
};
const result = await axios({
  url: "/a",
  method: "post",
  data: body,
  params: query,
  headers: {
    "Content-Type": "application/json",
  },
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/426939
推荐阅读
相关标签
  

闽ICP备14008679号