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

1
2
3
4
5
6
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

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
9
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 

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

  

 

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

posted @   文学少女  阅读(792)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示