【发布时间】:2018-02-11 22:18:01
【问题描述】:
当它到达服务器时,我的客户端应用程序不发送数据并显示undefined。我对节点和整体获取 api 相当陌生。当我点击前端的一个按钮时,它应该触发signin() & send username('robot') & password('cookies') 但它不起作用并且在终端中输出显示未定义。
服务器
var database = [
{
name: 'robot',
password: 'cookies'
}
];
app.post('/signin',(req,res)=>{
let username = req.body.username;
let password = req.body.password;
database.map((user,i) =>{
if (username === database[i].name &&
password === database[i].password) {
res.json('success');
}else{res.json("Login fail");console.log(req.body.username+":user:",req.body.username+":pass:");
}
});
});
前端
signIn() {
console.log('SignIn function called');console.log("Password: ",this.state.password);console.log("Username: ",this.state.username);
fetch('http://localhost:3000/signin',{
method:'post',
header:{'Content-Type':'application/json'},
body: JSON.stringify({
username:this.state.username,
password:this.state.password
})
})
.then(r => r.json())
.then(d => {
if (d === 'success'){
console.log("Login succesfull");
}else{
console.log("Failed");
}
})
}
【问题讨论】:
-
我已经在我的代码中使用了它
-
好像你在
FRONT-END中也粘贴了SERVER代码。 -
对不起,我只是编辑一下,请看我的新代码。
标签: javascript node.js fetch-api