axios.js 调用案例(post带url参数..)

axios.js 调用案例

post 请求发送json数据

“Content-Type”: “application/json”

const axios = require('axios');
const qs = require('qs');

axios.post(`http://localhost:8080/case/array`, {
     
    "id": "aaa"
  })
  .then(function (resp) {
     
    console.log("response", resp.data);
  }, (err) => {
     
    console.log(err);
  });

请求url:http://localhost:8080/case
请求体:{“id”: “aaa”}

post 请求发送表单数据

“Content-Type”: “application/x-www-form-urlencoded”

const axios = require('axios');
const qs = require('qs');

axios.post(`http://localhost:8080/case`, qs.stringify({
     
    "id": "aaa"
  }))
  .then(function (resp) {
     
    console.log("response", resp.data);
  }, (err) => {
     
    console.log(err);
  });

请求url:http://localhost:8080/case
请求体:id=aaa

post 请求带url参数(querystring)

const axios = require('axios');
const qs = require('qs');

axios.post(`http://localhost:8080/case`, {
     
    "id": "aaa"
  }, {
     
    params: {
     
      "name": "markix"
    }
  })
  .then(function (resp) {
     
    console.log("response", resp.data);
  }, (err) => {
     
    console.log(err);
  });

请求url:http://localhost:8080/case?name=markix
请求体:{“id”: “aaa”}

get 请求带请求体

axios.get(`http://localhost:8080/case`, {
     
    params: {
     
      "name": "markix"
    },
    data: {
     
      "id": "aaa"
    }
  })
  .then(function (resp) {
     
    console.log("response", resp.data);
  }, (err) => {
     
    console.log(err);
  });

请求url:http://localhost:8080/case?name=markix
请求体:{“id”: “aaa”}

你可能感兴趣的:(前端,axios,vue)