我编写了一个简单的表单,可以向服务器发送包含数据的post请求.单击submit按钮,我想清除表单,所以我创建了一个刷新页面的函数.该函数工作正常,但现在数据不会发送到服务器.
import React, {Component} from "react";
import axios from "axios";
class PostForm extends Component {
constructor(props) {
super(props);
this.state = {
id: '',
name: '',
surname: '',
country: ''
}
}
changeHandler = e => {
this.setState({[e.target.name]: e.target.value});
}
clearFunc = () => {
window.location.reload(false);
}
submitHandler = e => {
e.preventDefault();
console.log(this.state);
axios.post('http://localhost:8080/human/add/', this.state)
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error)
});
}
render() {
const {id, name, surname, country} = this.state;
return (
<div>
<form id="input-form" onSubmit={() => {
this.submitHandler();
this.clearFunc()
}}>
<div>
<input type="number" name="id" value={id} onChange={this.changeHandler}/>
</div>
<div>
<input type="text" name="name" value={name} onChange={this.changeHandler}/>
</div>
<div>
<input type="text" name="surname" value={surname} onChange={this.changeHandler}/>
</div>
<div>
<input type="text" name="country" value={country} onChange={this.changeHandler}/>
</div>
<button type="submit">Submit</button>
</form>
</div>
)
}
}
export default PostForm;