axios

92 阅读1分钟

axios

GET 请求111

// 拼接参数方式
 axios.get('/user?ID=12345')
  .then(function (response) {
  })
  .catch(function (error) 
  });
 // 传递对象方式
 axios.get('/user', {
   params: {
    ID: 12345
   }
  })
  .then(function (response) {
  })
  .catch(function (error) {
  });

POST 请求

axios.post('/user', {
   firstName: 'Fred',
   lastName: 'Flintstone'
  })
  .then(function (response) {
  })
  .catch(function (error) {
  });

执行多个并发请求

function getUserAccount() {
  return axios.get('/user/12345');
 }

 function getUserPermissions() {
  return axios.get('/user/12345/permissions');
 }

 axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
   // 两个请求现在都执行完成
  }));

通用请求方式

// 发送 POST 请求
 axios({
  method: 'post',
  url: '/user/12345',
  data: {
   firstName: 'Fred',
   lastName: 'Flintstone'
  }
 })
.then(function (response) {
  })
.catch(function (error) {
  });

默认配置

axios.defaults.method = 'get',

axios.defaults.baseURL = 'http://localhost:3000',

axios.defaults.timeout = 3000

创建实例对象

// 创建实例
let duanzi = axios.create({
      baseURL:"https://api.apiopen.top",
      timeout:3000
})
// 用实例发送请求
duanzi({ url:'/getJoke' }).then(res=>{ })