我正在尝试使用 axios 在 vue js 应用程序中向本地 API 发出发布请求,并且响应返回空数据。使用 Postman 工具,API 上的 post 请求工作正常。下面是我的代码
var the_data = {
title: 'This is title',
description: 'this is description'
}
axios.post('/api/snippets/insert', the_data)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
在 API 端,我使用一个简单的 PHP 脚本并使用此代码打印整个 $_POST 请求数据
var_dump($_POST);
但这返回空数组。
我也遇到了这个问题。 Axios 不会以您期望的形式发送 POST 数据。你需要类似的东西http://github.com/ljharb/qs http://github.com/ljharb/qs然后使用axios.post('/api/snippets/insert', Qs.stringify(the_data))
。请注意此构建在 cdnjs 上使用Qs
, not qs
.
替代品qs
会是例如JSON.stringify()
或者 jQuery 的$.param()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)