gpt4 book ai didi

javascript - 在 ReactJs 中使用 NodeJS API 登录失败

转载 作者:行者123 更新时间:2023-12-03 14:15:44 24 4
gpt4 key购买 nike

我正在使用 Nodejs 和 Express-session 在 ReactJs 中进行简单的用户登录。我遇到的问题是我的前端(React 登录页面)无法工作。这是我在 Login.js 中使用的 Fetch API:

  SubmitLogin (event) {
event.PreventDefault();
debugger;
fetch('http://localhost:4000/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type' : 'application/json'
},
body : JSON.stringfy (this.state)
}).then((Response) => Response.json())
.then((result) => {
console.log(result);
if (result.Status === 'Invalid')
alert('Invalid User');
else
this.props.history.push({Home});
alert('Login Sucessfull');
})
console.log(this.state);
alert("test input login")
}

为了连接到后端,我添加了 server.js,编码如下:

app.post('/login', jsonParser, (req, res) => {
var username = req.body.username;
var password = req.body.password;
if (username && password) {
dbConn.query(`SELECT * FROM user_tbl WHERE username = ? AND password = ?`, [username, password], (err, results, fields) => {
if (results.length > 0) {
req.session.loggedin = true;
req.session.username = username;
res.redirect('/home');
console.log(results)
} else {
res.send('Incorrect Username and/or Password!');
}
res.end();
});
} else {
res.send('Please enter Username and Password!');
res.end();
}
});

app.get('/home', (req, res) => {
if (req.session.loggedin) {
res.send('Welcome back, ' + req.session.username + '!');
} else {
res.send('Please login to view this page!');
}
res.end();
});

我已经使用 postman 测试了后端,并且它正在工作。请帮我提供一些建议以及如何放置 console.log 来查找 Login.js 中的错误。感谢您的帮助

postman 的结果: enter image description here

最佳答案

Response.json() 更改为 Response.text(),因为您在响应中返回 text 而不是 json,并且您可以添加 catch block 来处理错误。

我可以在您的代码中看到您在 Postman 中使用 Content-Type application/x-www-form-urlencodedfetch 调用中的 application/json。在两个请求中使用相同的 Content-Type

SubmitLogin(event) {
event.PreventDefault();
fetch('http://localhost:4000/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringfy(this.state)
}).then((Response) => Response.text())
.then((result) => {
console.log(result);
if (result.Status === 'Invalid')
alert('Invalid User');
else
this.props.history.push({ Home });
alert('Login Sucessfull');
}).catch(error => {
console.error(error)
})
console.log(this.state);
alert("test input login")
}

您可以更改代码以使用async/await以获得更好的可读性。

async SubmitLogin(event) {
event.PreventDefault();
try {
let response = await fetch('http://localhost:4000/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringfy(this.state)
});
let result = await response.text();
console.log(result);
if (result.Status === 'Invalid')
alert('Invalid User');
else
this.props.history.push({ Home });
alert('Login Sucessfull');
} catch (error) {
console.error(error.message);
}
console.log(this.state);
alert("test input login")
}

关于javascript - 在 ReactJs 中使用 NodeJS API 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60647892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com