REACT 获取 post 请求
REACT fetch post request
我在路由 post 请求时遇到问题
我需要建立注册表格和 post 从表格输入到 mongodb
我在服务器端制作了路由器和 post 路由,它工作正常(当我使用 postman 时)
//表格为必填模型
router.route('/').post(function(req,res,next){
res.send(req.body)
form.create(
{"first_name": req.body.first_name,
"last_name": req.body.last_name
})
.then(function(data){
res.send(data);
console.log(data);
}).catch(function(err){console.log(err)});
});
但我需要从客户端启动它,而不是 post人。在这里我迷路了。我可以做到,但是当我添加 onSubmit 操作时,它不起作用。而且我需要使用新功能来触发另一件事而不重定向到另一个页面。如何将 this.refs.first_name.value 传递给 body 以便我可以使用 fetch 函数??
下面的反应组件
添加了这个 JavaScript/JSON 片段
export default class Form extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
console.log(this.refs.first_name.value);
fetch('/', {
method: 'post',
body: {
"first_name": this.refs.first_name.value
}
});
};
render () {
return (
<div id="signup">
<form onSubmit={this.handleSubmit}>
<input ref="first_name" placeholder="First Name" type="text" name="first_name"/><br />
<input placeholder="Last Name" type="text" name="last_name"/><br />
<button type="Submit">Start</button>
</form>
</div>
)
}
}
我想您使用 ref
的方式已被弃用。试试下面看看你有没有运气。
export default class Form extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: {
"first_name": this.firstName.value
}
});
};
render () {
return (
<div id="signup">
<form onSubmit={this.handleSubmit}>
<input ref={(ref) => {this.firstName = ref}} placeholder="First Name" type="text" name="first_name"/><br />
<input ref={(ref) => {this.lastName = ref}} placeholder="Last Name" type="text" name="last_name"/><br />
<button type="Submit">Start</button>
</form>
</div>
)
}
}
这里是 link 反应文档 refs
您可以使用受控组件的概念。
为此,添加状态值,
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state={ firstname:"", lastname:""}
}
现在输入字段就像,
<input placeholder="First Name" type="text" value={this.state.firstname} onChange={(ev)=>this.setState({firstname:ev.target.value})}/>
<input placeholder="Last Name" type="text" value={this.state.lastname} onChange={(ev)=>this.setState({lastname:ev.target.value})}/>
handleSubmit 应该是这样的,
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: {
"first_name": this.state.firstName
}
});
};
我们需要将发送数据作为 json 字符串化
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({
"first_name": this.state.firstName
})
});
};
这就是我在 React.js 中提出 post 请求的方式;
const res = fetch('http://15.11.55.3:8040/Device/Movies', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: {
id: 0,
},
})
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});
我在路由 post 请求时遇到问题 我需要建立注册表格和 post 从表格输入到 mongodb 我在服务器端制作了路由器和 post 路由,它工作正常(当我使用 postman 时)
//表格为必填模型
router.route('/').post(function(req,res,next){
res.send(req.body)
form.create(
{"first_name": req.body.first_name,
"last_name": req.body.last_name
})
.then(function(data){
res.send(data);
console.log(data);
}).catch(function(err){console.log(err)});
});
但我需要从客户端启动它,而不是 post人。在这里我迷路了。我可以做到,但是当我添加 onSubmit 操作时,它不起作用。而且我需要使用新功能来触发另一件事而不重定向到另一个页面。如何将 this.refs.first_name.value 传递给 body 以便我可以使用 fetch 函数?? 下面的反应组件
添加了这个 JavaScript/JSON 片段
export default class Form extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
console.log(this.refs.first_name.value);
fetch('/', {
method: 'post',
body: {
"first_name": this.refs.first_name.value
}
});
};
render () {
return (
<div id="signup">
<form onSubmit={this.handleSubmit}>
<input ref="first_name" placeholder="First Name" type="text" name="first_name"/><br />
<input placeholder="Last Name" type="text" name="last_name"/><br />
<button type="Submit">Start</button>
</form>
</div>
)
}
}
我想您使用 ref
的方式已被弃用。试试下面看看你有没有运气。
export default class Form extends React.Component {
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: {
"first_name": this.firstName.value
}
});
};
render () {
return (
<div id="signup">
<form onSubmit={this.handleSubmit}>
<input ref={(ref) => {this.firstName = ref}} placeholder="First Name" type="text" name="first_name"/><br />
<input ref={(ref) => {this.lastName = ref}} placeholder="Last Name" type="text" name="last_name"/><br />
<button type="Submit">Start</button>
</form>
</div>
)
}
}
这里是 link 反应文档 refs
您可以使用受控组件的概念。
为此,添加状态值,
constructor(props){
super(props);
this.handleSubmit = this.handleSubmit.bind(this);
this.state={ firstname:"", lastname:""}
}
现在输入字段就像,
<input placeholder="First Name" type="text" value={this.state.firstname} onChange={(ev)=>this.setState({firstname:ev.target.value})}/>
<input placeholder="Last Name" type="text" value={this.state.lastname} onChange={(ev)=>this.setState({lastname:ev.target.value})}/>
handleSubmit 应该是这样的,
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: {
"first_name": this.state.firstName
}
});
};
我们需要将发送数据作为 json 字符串化
handleSubmit(event){
event.preventDefault();
fetch('/', {
method: 'post',
headers: {'Content-Type':'application/json'},
body: JSON.stringify({
"first_name": this.state.firstName
})
});
};
这就是我在 React.js 中提出 post 请求的方式;
const res = fetch('http://15.11.55.3:8040/Device/Movies', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: {
id: 0,
},
})
.then((response) => response.json())
.then((responseJson) => {
return responseJson.movies;
})
.catch((error) => {
console.error(error);
});