axios POST提交数据的三种请求方式写法

常用的三种header中的三种content-type如下

1、Content-Type: application/json

2、Content-Type: multipart/form-data

3、Content-Type: application/x-www-form-urlencoded

项目中第二次遇到需要独立设置header中的content-type,故查阅而记之

1、Content-Type: application/json

import axios from 'axios'
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/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');
axios.post(`${this.$url}/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"};
axios.post(`${this.$url}/test/testRequest`,qs.stringify({
    data
}))
.then(res=>{
    console.log('res=>',res);            
}) 

 

axios修改content-type 

axios.post(url, params, {
    headers: {
      'Content-type':'application/x-www-form-urlencoded'
    }
  })

  

 

 参阅: https://segmentfault.com/a/1190000015261229

posted @ 2020-04-14 14:19  文学少女  阅读(784)  评论(0编辑  收藏  举报