Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在reacts组件中调用POST请求将接收Props_Javascript_Ajax_Reactjs_Axios - Fatal编程技术网

Javascript 在reacts组件中调用POST请求将接收Props

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

我正在尝试以react生命周期方法提交POST请求以保存到我的数据库

我的组件WillReceiveProps如下所示:

// 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配置问题?