1. 程式人生 > >vue---進行post和get請求

vue---進行post和get請求

參考文件:

https://www.jb51.net/article/125717.htm

使用axios

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

基本使用方法:

get請求:

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
 .then(function (response) {
  console.log(response);
 })
 .catch(function (error) {
  console.log(error);
 });
 
// Optionally the request above could also be done as
axios.get('/user', {
  params: {
   ID: 12345
  }
 })
 .then(function (response) {
  console.log(response);
 })
 .catch(function (error) {
  console.log(error);
 });
 

Post請求:

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

簡單示例:

// 在進行 post 和 get 請求的時候,使用 axios 進行訪問
// 進行 get 請求
axios.get(url).then(function (response){
    console.log(response);
}).
catch(function(error){ console.log(error); }); // 進行post 請求 axios.post(url,{firstName:'Fred',lastName:'Flintstone'}).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); });