1.進入到項目目錄中,安裝axios
npm install axios --save
2. main.js中引入axios
3. 在組件中發(fā)起請求
3.1 get請求
// 請求時傳遞的參數(shù)params
let params = {a:5,b:3}
this.$axios.get('/url', {params:params}).then(res => {
console.log(res);
}).catch(err => {
console.log(err)
})
3.2 post請求
- 【1】Content-Type: application/json
import axios from 'axios'
let data = {"code":"1234","name":"yyyy"};
this.$axios.post('/test/testRequest', data)
.then(res=>{
console.log('res=>',res);
})
- 【2】Content-Type: multipart/form-data
import axios from 'axios'
let data = new FormData();
data.append('code','1234');
data.append('name','yyyy');
this.$axios.post('/test/testRequest', data)
.then(res=>{
console.log('res=>',res);
})
- 【3】Content-Type: application/x-www-form-urlencoded
import axios from 'axios'
import qs from 'Qs'
let data = {"code":"1234","name":"yyyy"};
this.$axios.post('/test/testRequest', qs.stringify(data), {headers:{'Content-Type':'application/x-www-form-urlencoded'}})
.then(res=>{
console.log('res=>',res);
})