【发布时间】:2018-05-17 11:37:18
【问题描述】:
所以基本上我有一个登录,每次用户按下登录按钮时都会发送帖子数据
sendLogin(e){
e.preventDefault();
fetch('/users', {
method: 'POST',
body: JSON.stringify({
email: this.email.value,
password: this.password.value
}),
}).then(function(response) {
return response
}).then(function(body) {
console.log(body);
});
}
render(){
return (
<form onSubmit={(event) => this.sendLogin(event)} class="asdf">
<input type="text" placeholder="email" ref={(input) => { this.email = input}}/>
<input type="password" placeholder="password" ref={(input) => { this.password = input }}/>
<input type="submit" placeholder="submit"/>
</form>
)
}
在我的 express server.js 代码中,我将其设置为控制台记录我们获得的数据
app.post('/users', function(req, res) {
var user = req.body;
res.end('Success');
console.log('GOT A REQUEST');
console.log(user);
})
console.log(user) 部分根本不工作,在客户端代码中 console.log(body);部分也根本不起作用。关于如何解决这个问题的任何想法?
编辑:它们都未定义
当我将返回响应更改为返回 response.json 时,它给了我这个错误
Uncaught (in promise) SyntaxError: Unexpected token S in JSON at position 0
Promise rejected (async)
【问题讨论】: