Javascript 在reacts组件中调用POST请求将接收Props
我正在尝试以react生命周期方法提交POST请求以保存到我的数据库 我的组件WillReceiveProps如下所示:Javascript 在reacts组件中调用POST请求将接收Props,javascript,ajax,reactjs,axios,Javascript,Ajax,Reactjs,Axios,我正在尝试以react生命周期方法提交POST请求以保存到我的数据库 我的组件WillReceiveProps如下所示: // Update location when user logs in componentWillReceiveProps(nextProps) { if (this.props.user.username !== nextProps.user.username) { if( navigator && navigator.geoloc
// Update location when user logs in
componentWillReceiveProps(nextProps) {
if (this.props.user.username !== nextProps.user.username) {
if( navigator && navigator.geolocation ) {
navigator.geolocation.getCurrentPosition((pos) => {
const { latitude, longitude } = pos.coords;
this.setState({
currentLocation: { lat: latitude, lng: longitude }
});
// save location...
this.saveLocation(latitude, longitude);
});
}
}
}
// Save to Database
saveLocation(lat, lng) {
console.log('saving.....');
axios.post('/location', { lat, lng })
.then((response) => {
console.log("saved");
}).catch((error) => {
console.log('not saved', error);
});
}
上面的post请求将拒绝连接到我的服务器。奇怪的是,我能够在componentDidMount中运行saveLocation,当通过curl发布时,同样的post请求也会起作用。在使用ajax和react时,我是否遗漏了一些必要的东西。任何建议都将不胜感激。控制台中的错误内容是什么?未保存错误:XMLHttpRequest.handleError(xhr.js?14ed:87)的createError(createError.js?f777:16)处的网络错误可能是服务器上的CORS配置问题,NGINX?控制台中的错误内容是什么?未保存错误:XMLHttpRequest.handleError(xhr.js?14ed:87)处的createError(createError.js?f777:16)处的网络错误可能是服务器NGINX上的CORS配置问题?