vue使用axios請求后端數據


1. 安裝axios

$ npm install axios

2.在main.js里面導入axios

import axios from 'axios'

Vue.prototype.$http = axios

3.下面就可以使用axios獲取數據了

// 發送get請求

// 向具有指定ID的用戶發出請求
this.$http.get('/user?ID=12345')
  .then(response => {
    console.log(response);
  })
// 錯誤處理
  .catch(error => {
    console.log(error);
  });
// 也可以通過 params 對象傳遞參數
$http.get('/user', {
    params: {
      ID: 12345
    }
  })
   .then(response => {
    console.log(response);
  })
// 錯誤處理
  .catch(error => {
    console.log(error);
  });
// 發送post請求
this.$http.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(response => {
    console.log(response);
  })
  .catch(error => {
    console.log(error);
  });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM