Vue将数据传递到POST方法
作者:互联网
我在将数据传递到Vuejs post方法时遇到问题.我正在使用vue-resource,在文档中是:
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
我想把标题和它工作正常(服务器读取标题正确):
this.$http.post(
'http://localhost:8081/login',
{
headers: {
'Accept': 'application/json'
}
});
但是如果我尝试添加任何数据,就会出问题,例如,我尝试过:
this.$http.post(
'http://localhost:8081/login',
{username: 'admin'},
{
headers: {
'Accept': 'application/json'
}
});
或定义一些数据,然后像这样放置:
this.$http.post(
'http://localhost:8081/login',
this.data,
{
headers: {
'Accept': 'application/json'
}
});
对于这两个解决方案,正文部分始终为空-使用以下命令检查请求中的正文数据:
body = req.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
可能的问题是在vue.js代码中,因为它可以正常运行,因为我发送了来自邮递员的任何请求.我该怎么办?
解决方法:
您是否尝试过发送发布请求,而没有在发布中包含第三个参数
this.$http.post('url', {something: "string"})
.then ((res)=> console.log (res.body))
.catch ((error)=> console.log(error))
这对我有用…
另一件事,您确定要在后端正确获取请求数据吗……好像是python,但我不确定该如何工作.
标签:vue-js,javascript 来源: https://codeday.me/bug/20191111/2021692.html