Vue项目中使用axios发送ajax请求

首先安装axios

npm install axios -S

在main.js中引入

import axios from 'axios'
Vue.prototype.$axios = axios

发送Get请求

this.$axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

// 上面的请求也可以这样做
this.$axios.get('/user', {
    params: {
      ID: 12345
    }
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

发送Post请求

this.$axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
posted @ 2020-11-29 16:53  Mirage_c  阅读(530)  评论(0)    收藏  举报