传递对象时,Axios POST请求不起作用

时间:2018-02-17 09:24:53

标签: vue.js axios

我正在尝试使用axios到本地API在vue js app中发布一个post请求,并且响应返回空数据。 API上的post请求使用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);

但这是返回空数组。

1 个答案:

答案 0 :(得分:2)

我也遇到了这个问题。 Axios不会以您期望的形式发送POST数据。您需要http://github.com/ljharb/qs之类的内容,然后使用axios.post('/api/snippets/insert', Qs.stringify(the_data))。请注意,基于cdnjs的内容使用Qs,而非qs

qs的替代方案将是JSON.stringify()或jQuery的$.param()

相关问题