更新网址并保存axios响应

时间:2020-08-10 00:52:44

标签: javascript reactjs url redirect axios

我有我的代码来接受用户(设备名称)的输入,然后单击View,然后将axios请求发送到服务并从中获得响应,并使用该响应为该示例设计设备名称。 此功能正常工作。

 <input type="text" id="devicename" onChange={this.onInputChange.bind(this)}><br> />
 <Button text='View' variant='primary' type='submit' onClick={this.onDeviceNameEntered} />

点击“查看”按钮时,将在onDeviceNameEntered处发送

onDeviceNameEntered = async () => {
    this.setState({ enteredDeviceName: true });
    const DeviceName = this.state.devicename;
    // an axios post request is built by calling buildRequestToGetData(DeviceName) 
    const responsedata = await axios.request(buildRequestToGetData(DeviceName)).then((response) =>
      (response.data),
    ).catch((error) => {
      if (error.response && error.response.data) {
        this.setState({ error: true });
      }
    }
    );
  //code to display the data in responsedata
  };

如果我需要从当前网址更改网址: https://www.example.com/https://www.example.com/DeviceName 在获得输入设备名称的axios响应之后,因此当我与其他人共享url时,他将能够看到特定设备的响应数据,而无需键入设备名称并再次单击“提交”按钮。 即如果我需要更新我的网址,并且需要在获得响应后共享该网址。是否有任何功能可以帮助我使用Javascript做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用df.unstack()检查网址的路径名。

在您的示例中,它将返回window.location.pathname。从路径名字符串中删除正斜杠,然后在状态下将路径名设置为设备名。

/DeviceName

state= { devicename: removeForwardSlash(window.location.pathname) } 只是实现删除正斜杠或解析路径名的功能,但是最适合您。

使用componentDidMount检查设备名称是否存在,然后使用removeForwardSlash进行api调用