vue.js - 传递对象时 Axios POST 请求不起作用

标签 vue.js axios

我正在尝试在 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/

相关文章:

javascript - Vuex 中如何访问 mapState 并渲染到 View

vue.js - vue router next() 函数在 router.beforeEach 中的 Promise 中不起作用

javascript - Vue 和 Vuex : Handling dependent computed properties

javascript - Fetch API 返回 415(不支持的媒体类型)

laravel-5 - 类型错误 : Cannot set property 'posts' of undefined - Vuejs

react-native - 根据用户的身份验证重定向用户

javascript - 更改组件的 html 元素类型

css - vue 基于媒体查询的绑定(bind)值

vue.js - Uncaught ReferenceError : photoModule is not defined

javascript - 需要使用 Vue.js 访问对象数组