美文网首页工作生活
10.vue.js 2.0 中使用axios向后台发送请求

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

作者: Ching_Lee | 来源:发表于2019-07-04 20:56 被阅读0次

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请求
  • 【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);            
})

相关文章

网友评论

    本文标题:10.vue.js 2.0 中使用axios向后台发送请求

    本文链接:https://www.haomeiwen.com/subject/gmrrhctx.html