场景
我遇到了4个小时的问题,我正在尝试在发送用户ID作为参数的同时发送http get请求。我尝试了很多在网上找到的示例,但后端仍然出现此错误。
GET http://localhost:3000/api/users/getusersbyid/?userId=00c1308a-32ad-48a0-8737-d4682b2b504e 500 (Internal Server Error)
这是我的 JS 功能代码:
async function getUserById() {
try {
await $.ajax({
type: "GET",
url: "http://localhost:3000/api/users/getusersbyid",
data: {
userId: "00c1308a-32ad-48a0-8737-d4682b2b504e"
},
contentType: "application/x-www-form-urlencoded"
}).done(function(response) {
console.log(response);
}).fail(function(err) {
console.log(err);
});
} catch (error) {
console.log(error);
}
}
这是我使用 NodeJs 的后端功能代码:
getUserById: function(req, res) {
let userId = req.body.userId;
models.User.findOne({
where: {
id: userId
},
include: [{
model: models.TypeUser,
attributes: ['code', 'value']
}]
}).then(function(data) {
if (data) {
res.status(201).json({
'status': 'success',
'code': 201,
'data': data
});
} else {
res.status(404).json({
'status': 'falled',
'code': 404,
'message': 'Unable to find one or more users'
});
}
}).catch(function(err) {
res.status(500).json({
'status': 'falled',
'code': 500,
'message': 'An internal error has occurred',
'data': err
});
});
}
Here is my Backend Error Message image:
需要您的帮助和建议
答案 0 :(得分:0)
您的后端似乎正在发生某些事情。您是否尝试过使用日志记录,例如在“ let userId = req.body.userId;”之后行来查看您的服务器是否正在接收userId?
console.log("backend received userId="+userId)
答案 1 :(得分:0)
在阅读了@AbhishekKumawat和@Pointy的答案之后,我才解决了问题。因此,使用“ GET ”方法,我应该这样做:
let userId = req.query.userId;
相反。
let userId = req.body.userId;