简体   繁体   English

使用 axios 响应发布请求无法正常工作

[英]react post request using axios is not working properly

I have few data and now i want to save it in database using axios post in react.我的数据很少,现在我想在反应中使用 axios 将其保存在数据库中。

I have two fields but in both fields coming same data, dont know whats wrong here.我有两个字段,但在两个字段中都有相同的数据,不知道这里出了什么问题。

My Code:我的代码:

 state = { salonName: '', locationCity: '', }; handleChange = event => { this.setState({ salonName: event.target.value }); this.setState({ locationCity: event.target.value }); }; handleSubmit = event => { event.preventDefault(); const salonData = { salonName: this.state.salonName, locationCity: this.state.locationCity, }; axios({ method: "post", url: "http://localhost:4200/addsalon", headers: {}, data: { salonName: salonData.salonName, locationCity: salonData.locationCity, }, }); } render() { return ( <div> <input type="text" className="form-control" placeholder="Enter Salon Name..." name="salonName" onChange={this.handleChange} /> <input type="text" className="form-control" placeholder="Enter City" name="locationCity" onChange={this.handleChange} /> </div>

but i'm getting same value in database like given below:- {"_id":"60ac7dd4ccfa1c07a8df84d8","salonName":"Delhi","locationCity":"Delhi","__v":0}]但我在数据库中得到相同的值,如下所示:- {"_id":"60ac7dd4ccfa1c07a8df84d8","salonName":"Delhi","locationCity":"Delhi","__v":0}]

ThankYou for your efforts!感谢你付出的努力!

The problem in handle change.手柄变化的问题。 you updating both with same if anyone changes如果有人更改,您将同时更新两者

 handleChange = event => {
        if(event.target.name ===='salonName'){
        this.setState({ salonName: event.target.value });
        }else if(event.target.name ===='locationCity'){
        this.setState({ locationCity: event.target.value });
        }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM