使用axios请求接口,几种content-type的区别详解

2020-06-12 20:52:25易采站长站整理

Form Data
{"firstName":"Fred","lastName":"Flintstone"}:

还有一种常见情况, 通过qs库对数据进行编码(前提要安装qs)


import qs from 'qs'
axios({
url:'/api/connect/token',
method: 'post',
data: qs.stringify({
firstName: 'Fred',
lastName: 'Flintstone'
})
}).then(res => {
console.log(1234, res.data)
}).catch(error => {
console.log(error)
})

结果:


Request Headers
Content-Type: application/x-www-form-urlencoded


Form Data
firstName: Fred
lastName: Flintstone

使用qs要注意的点 :

allowDots(多层对象嵌套, 可用.标记)


qs.stringify({
a: {
b: {
c: 'd', e: 'f'
}
}
}, { allowDots: true });
// 'a.b.c=d&a.b.e=f'

以上这篇使用axios请求接口,几种content-type的区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。