使用axios遇到一个坑,发出的post请求后台通过$_POST
怎么也拿不到数据。
原因:$_POST
只能获取通过表单的方式提交的数据,即请求头的Content-Type为application/x-www-form-urlencoded,而axios.post提交的数据请求头中的Content-Type默认是application/json。
解决方案:
- 使用qs模块并规定请求的Content-Type
let obj = {
name: '',
psd: ''
};
let url = '';
axios.post(url,qs.stringify(data),{
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(result => {
// do something
})
2.使用 php://input 流来获取数据