使用axios post数据时,
需设置header选项的不同情况:
因为axios默认的提交json格式,但后台是表单接收,所以需要设置header
headers:{//上传文件时,文件是通过表单data提交
'Content-type': 'multipart/form-data'
}
headers:{//普通发送数据时
'Content-type': 'application/x-www-form-urlencoded'
}
需修改data选项:
使用qs模块转换数据为字符串连接格式,例如:
data: this.$qs.stringify(senddata)//向后台传输数据时,因后台默认接收表单数据
一份完整的使用post发送数据的示例:
this.$axios({
method: 'post',
url: this.$conf.url+'/kuayu',
data: this.$qs.stringify(senddata),//把json格式的数据stringify
headers:{//修改header头
'Content-type': 'application/x-www-form-urlencoded'
}
})
.then(function (response)
{
console.log(response);
})
.catch(function (error) {
console.log(error);
});
封装一个axios的对象,方便以后调用