这是我在该网站上提出的第一个问题。
我为一个学校项目创建了自己的 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 调用的方式:
客户:
How I call in the component client part1
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)
}
API:
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 wait 并遇到了同样的问题)。
有人有想法吗,我以为是因为它没有经过我的路线,但我不明白为什么。
感谢您的帮助:)
最佳答案
您在此处使用 GET 请求:
login (credentials) {
return Api.get('/AREA/User', credentials)
}
GET 请求不能有正文。
如果您想传递正文,则必须使用 POST 请求或其他请求类型之一。
对于 GET 请求,正文的替代方法是使用查询参数。这些仅以 ?param=value
形式附加到 URL 末尾。我不知道 Api.get
是如何实现的,因此无法给出有关如何向其传递查询参数的具体建议。
关于express - 在 API(Vue.js、express.js)上使用 get 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59428598/