默认提交payload
以json形式提交
axios.post(url, {key: value}
).then( res => {
console.log(res)
}).catch( res => {
console.log(res)
})
提交formdata
如果后端人员比较老派,没法接受json格式,那就给他提交formdata
var fd = new FormData()
fd.append('key', value)
let config = {
headers: {
'Content-Type': 'multipart/form-data'
}
}
axios.post(url, fd, config
).then( res => {
console.log(res)
}).catch( res => {
console.log(res)
})
或者使用CDN方式引入
let url = "http://127.0.0.1"
let data = {
name: "Tom",
age: 23
}
// 注意Qs是大写,和npm引入方式不一样
axios.post(url, Qs.stringify(data)).then(res=>{
console.log(res);
}).catch(err=>{
console.log(err);
})
参考 axios配置后,post提交formdata