Javascript Req.body返回的对象类型错误
我正在向后端发送一个简单的注册表(POST fetch)进行处理。然而,请求主体并没有收到应有的接收方式 我希望看到request.body={“username”:“john”,“password”:“password”} 但是当我记录它的时候我看到了 {{“用户名”:“汽车”,“密码”:“汽车”}':''} 这是我的回执:Javascript Req.body返回的对象类型错误,javascript,reactjs,express,fetch,Javascript,Reactjs,Express,Fetch,我正在向后端发送一个简单的注册表(POST fetch)进行处理。然而,请求主体并没有收到应有的接收方式 我希望看到request.body={“username”:“john”,“password”:“password”} 但是当我记录它的时候我看到了 {{“用户名”:“汽车”,“密码”:“汽车”}':''} 这是我的回执: fetchRegister = (e) => { //triggered when submitting Register Info e.prevent
fetchRegister = (e) => { //triggered when submitting Register Info
e.preventDefault();
const registerOptions = {
method: "POST",
mode: "no-cors",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: JSON.stringify({
username: this.state.regUsername, //The inputChange function will set these to whatevr the user types
password: this.state.regPassword,
}),
}
fetch("http://localhost:5000/register", registerOptions)
.then(response => response.json())
.then(data =>
console.log(data)
)
}
这是我的终点:
app.use(bodyParser.urlencoded({ extended: true}))
app.use(bodyParser.json());
app.post('/register', (req, res) => {
console.log(req.body);
const inputUsername = req.body.username;
const inputPassword = req.body.password;
let newUser = new User({
userName: inputUsername,
password: inputPassword
})
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, function(err, hash) {
if(err) {
res.send(err);
}
newUser.password = hash;
newUser.save((err) => {
if (err) {
console.log(err)
res.send(JSON.stringify({'message': 'Username is already taken'}));
}else {
res.send(JSON.stringify({'message': 'you were successful'}));
}
})
});
})
})
<div id="registerForm">
<form>
<legend>Register:</legend>
Choose a Username:<input className="reg" value={this.state.regUsername} onChange={this.inputChange("regUsername")} required></input>
Choose your Password:<input className="reg" type="password" value={this.state.regPassword} onChange={this.inputChange("regPassword")} required></input>
<button className="FORMbtn" onClick={this.fetchRegister} type="submit">Register Me</button>
</form>
</div>
app.use(bodyParser.urlencoded({extended:true}))
use(bodyParser.json());
应用程序发布(“/寄存器”,(请求,回复)=>{
控制台日志(请求主体);
const inputUsername=req.body.username;
const inputPassword=req.body.password;
让newUser=newUser({
用户名:inputUsername,
密码:inputPassword
})
bcrypt.genSalt(10,(错误,盐)=>{
bcrypt.hash(newUser.password、salt、函数(err、hash){
如果(错误){
res.send(err);
}
newUser.password=hash;
newUser.save((错误)=>{
如果(错误){
console.log(错误)
res.send(JSON.stringify({'message':'Username's ready'take}));
}否则{
res.send(JSON.stringify({'message':'youaresccessful'}));
}
})
});
})
})
登记册:
选择一个用户名:
选择您的密码:
给我登记
我只希望一个标准对象被发送到我的后端。我不知道是CORS还是我的应用程序类型搞砸了。它在《邮递员》中的效果非常好。这是CORS发行的。我必须用NPM安装“cors”,并将其引入Express。然后我可以去掉“mode”:“no cors”,它就成功了。你忘了为json添加bodyparser。或者,不管出于什么原因,它都没有在这个请求上运行。(
“内容类型”:“application/x-www-form-urlencoded”,
将是一个很好的理由)您的内容类型与您发送的内容不匹配。能否详细说明内容类型问题?它甚至不会发送,如果它不是那样的内容类型哦,它会发送的很好。但是服务器无法正确解释它。下一步应该尝试哪种内容类型?我尝试了不起作用的“application/json”