工作生活

10.vue.js 2.0 中使用axios向后台发送请求

2019-07-04  本文已影响0人  Ching_Lee

1.进入到项目目录中,安装axios

npm install axios --save

2. main.js中引入axios

3. 在组件中发起请求

3.1 get请求
// 请求时传递的参数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请求
import axios from 'axios'
let data = {"code":"1234","name":"yyyy"};
this.$axios.post('/test/testRequest', data)
.then(res=>{
    console.log('res=>',res);            
})
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);            
})
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);            
})
上一篇下一篇

猜你喜欢

热点阅读