fetch:undefined 从正文解析器获取的数据(Express、node js)
fetch:undefined data getting from body parser (Express,node js)
我是 React js 和 node js 的新手。这是一个简单的问题,但我不知道如何解决 it.I 我正在使用对 post 数据和接收而言重量轻的 fetch来自 bodyparser.But 的数据始终未定义
form.js
import React from 'react';
import Fetch from 'whatwg-fetch';
class Form extends React.Component{
constructor(props){
super(props);
this.state = {
value:'',
posted:''
}
}
onPost(e){
e.preventDefault();
// console.log(this.refs.email.value);
// console.log(this.refs.name.value);
if(this.refs.email.value && this.refs.name.value != ''){
fetch('/post',{
method:'POST',
body:JSON.stringify({
email:this.refs.email.value,
name:this.refs.name.value
})
})
}
}
render(){
console.log(this.state);
return (
<div>
<form className="postform" onSubmit={this.onPost.bind(this)}>
<label>Email Addres</label>
<input type="email" ref="email" className="form-control"/>
<label>Name</label>
<input type="text" ref="name" className="form-control"/>
<br/>
<br/>
<button type="submit" className="btn btn-primary">Post To server</button>
</form>
</div>
)
}
}
export default Form;
server.js
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
const jsonParser = bodyParser.json();
app.set('view engine','ejs');
app.use(express.static('./public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('*',(req,res)=>{
res.render('index')
});
app.post('/post',(req,res)=>{
console.log(req.body);//undefines
res.send('Ok ')
});
app.listen(3000,err=>{
if(err) throw err;
console.log('Im In at 3000')
});
试试这个:
替换
app.use(bodyParser.urlencoded({ extended: false }));
和
app.use(bodyParser.urlencoded());
或
app.use(bodyParser.urlencoded({ extended: true }));
我是 React js 和 node js 的新手。这是一个简单的问题,但我不知道如何解决 it.I 我正在使用对 post 数据和接收而言重量轻的 fetch来自 bodyparser.But 的数据始终未定义
form.js
import React from 'react';
import Fetch from 'whatwg-fetch';
class Form extends React.Component{
constructor(props){
super(props);
this.state = {
value:'',
posted:''
}
}
onPost(e){
e.preventDefault();
// console.log(this.refs.email.value);
// console.log(this.refs.name.value);
if(this.refs.email.value && this.refs.name.value != ''){
fetch('/post',{
method:'POST',
body:JSON.stringify({
email:this.refs.email.value,
name:this.refs.name.value
})
})
}
}
render(){
console.log(this.state);
return (
<div>
<form className="postform" onSubmit={this.onPost.bind(this)}>
<label>Email Addres</label>
<input type="email" ref="email" className="form-control"/>
<label>Name</label>
<input type="text" ref="name" className="form-control"/>
<br/>
<br/>
<button type="submit" className="btn btn-primary">Post To server</button>
</form>
</div>
)
}
}
export default Form;
server.js
import express from 'express';
import bodyParser from 'body-parser';
const app = express();
const jsonParser = bodyParser.json();
app.set('view engine','ejs');
app.use(express.static('./public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('*',(req,res)=>{
res.render('index')
});
app.post('/post',(req,res)=>{
console.log(req.body);//undefines
res.send('Ok ')
});
app.listen(3000,err=>{
if(err) throw err;
console.log('Im In at 3000')
});
试试这个:
替换
app.use(bodyParser.urlencoded({ extended: false }));
和
app.use(bodyParser.urlencoded());
或
app.use(bodyParser.urlencoded({ extended: true }));