我正在学习react-native,开发登录功能时遇到一些问题
这是我在react-native中的获取函数
getLogin = () => {
fetch("http://localhost:5000/auth/login", {
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: `${this.state.email}`,
pass: `${this.state.pass}`,
}),
})
.then((res) => res.json())
.then((data) => {
console.log(this.state)
console.log(data)
if (!this.state.success) {
console.log(data)
console.log("Login false!");
}
else {
console.log(data);
console.log("Login success");
}
})
.catch((error) => {
console.error(error);
});
}
这是我在服务器中的代码
module.exports.postLogin = (req, res) => {
console.log(req)
var email = req.body.email
var pass = req.body.pass
var user = db.get('users').find({ email: email }).value()
if (!user) {
res.json({
errors: [
"user does not exist "
],
values: req.body,
})
}
if (user.pass !== pass) {
res.json({
errors: [
"wrong pass "
],
values: req.body,
})
}
res.json({
success: true
})
res.cookie("userID", user.id, {
signed: true
})
}
我无法向服务器发送数据,我也不能,也没有从服务器接收数据 请帮助我,谢谢你的sp
最佳答案
您有两种解决方案:
使用您计算机的IP 地址而不是本地主机,并确保您的计算机以及您的设备和模拟器在同一网络中运行
解决方案2:您可以使用ngrok https://ngrok.com/也就是说,将为在端口:5000 运行的服务器代码生成一个 HTTPS URL,并且可以从其他设备访问它
关于node.js - 我无法向服务器发送数据,也不能,也没有从服务器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58743059/