[英]Error with using get on an API (Vue.js, express.js)
这是我在这个网站上的第一个问题。
我为一个学校项目创建了自己的 API,路径很少:
app.get('/AREA/', (req, res) => res.status(200).send({
message: 'Welcome to the AREA API'
}));
app.get('/AREA/Users', User.list);
app.post('/AREA/User', AuthPolicies.register, User.create);
app.get('/AREA/User', User.recup);
这就是我调用最后一个 get 和 post 调用的方式:
客户:
RecupUser: function () {
this.error = ''
const User = {
email: this.email,
password: this.password
}
AuthServices.login(User)
.then(res => {
console.log(res)
})
.catch(err => {
console.log(err)
})
}
register (credentials) {
return Api.post('/AREA/User', credentials)
},
login (credentials) {
return Api.get('/AREA/User', credentials)
}
应用程序接口:
create(req, res) {
return User
.create({
'email': req.body.email,
'password': req.body.password
})
.then(response => res.status(200).send(response))
.catch(res.status(400).send({
error: 'This email account is already in use.'
}));
},
recup(req, res) {
return User
.findOne({where: {email: req.body.email},})
.then(user => res.status(200).send({
user: user.toJSON()
}))
.catch(err => res.status(400).send({
error: err
}));
}
错误恢复:
Error: "Request failed with status code 400"
createError createError.js:16
settle settle.js:17
handleLoad xhr.js:59
Login.vue:62
除了 app.get('/AREA/User', User.recup) 之外的所有工作,我真的很想找出原因(我也尝试过 asyc await 并遇到了同样的问题)。
有人有想法吗,我以为是因为它没有经过我的路线,但我不明白为什么。
谢谢您的帮助 :)
您在此处使用 GET 请求:
login (credentials) {
return Api.get('/AREA/User', credentials)
}
GET 请求不能有正文。
如果要传递正文,则必须使用 POST 请求或其他请求类型之一。
对于 GET 请求,替代正文是使用查询参数。 这些只是以?param=value
的形式附加到 URL 的末尾。 我不知道Api.get
是如何实现的,所以我无法就如何将查询参数传递给它提供具体建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.