我正在尝试在 vue js 应用程序中使用 axios 向本地 API 发出发布请求,响应返回空数据。 API 上的发布请求使用 Postman 工具运行良好。下面是我的代码
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 的东西然后使用 axios.post('/api/snippets/insert', Qs.stringify(the_data))
。请注意,这个基于 cdnjs 的构建使用 Qs
,而不是 qs
。
qs
的替代方案是例如JSON.stringify()
或 jQuery 的 $.param()
。
关于vue.js - 传递对象时 Axios POST 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48839798/