我在使用 Axios 和使用 Laravel 路由的参数对象发帖时遇到问题。
如果我使用查询参数例如?username=user
发布请求有效。但是,如果我使用一个对象,它就会失败:
这是 Axios 发布请求:
let params =
{
'username': 'help',
'password': 'me'
};
axios.post(
'https://cors.now.sh/https://jumprope.design/test-login', params, {
headers: {
'accept': 'application/json',
'accept-language': 'en_US',
'content-type': 'application/x-www-form-urlencoded',
'_token': 'UawpqT74cr9sr0Uut0lttZPE2YKDb1ckvXYpzNJW'
}
}).then(function(response) {
console.log(response);
})
.catch(function(error) {
console.log(error);
});
当我删除参数 obj 并将 ?username=user
附加到 URL 路由时,它起作用了,我从请求中返回了数据。
这是 Laravel (5.1) Controller :
protected function postTestRequest(Request $request)
{
$data = $request::all();
return response()->json($data);
}
在浏览器中点击此路由或使用 postman 进行测试工作正常:https://cors.now.sh/https://jumprope.design/test-login
这里还有代码笔的链接:CodePen
非常感谢您的帮助或指导。
此外,我已经尝试过这里的建议 https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format但仍然收到相同的结果。
最佳答案
检查你的路由文件并查看你想要定位的路由是否使用“POST”方法定义,因为你正在使用“POST”并检查 该路线针对“postTestRequest”方法。你的路线肯定有问题。还要检查是否有任何其他路由与其冲突。
关于javascript - 使用带参数对象的 Axios Post 没有返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259241/