Ruby on rails 当URL在ReactJS+;上具有动态参数时,axios使用了错误的URL;轨道

Ruby on rails 当URL在ReactJS+;上具有动态参数时,axios使用了错误的URL;轨道,ruby-on-rails,reactjs,axios,Ruby On Rails,Reactjs,Axios,因此,我使用axios将reactJS前端与rails API后端连接起来 这是我的路由器: [...] <Route path="/" component={LandingPage} exact={true}/> <Route path="/meals" component={MealsPage} exact={true}/> <Route exact path="/user/:token" render={routeP

因此,我使用axios将reactJS前端与rails API后端连接起来

这是我的路由器:

[...]
<Route path="/" component={LandingPage} exact={true}/>
<Route path="/meals" component={MealsPage} exact={true}/>
<Route exact path="/user/:token"
                      render={routeProps => (
                        <MyAccountPage {...routeProps} currentUser={this.state} />
                      )}
              />
[...]
它与
/
/founds
路线完美配合。但是当使用
/user/:token
调用axios时,它会尝试从
/user/api/v1/users/current
而不是
/api/v1/users/current
获取数据


如何更改它,使其同时适用于dev和prod环境?

问题在于您的
axios.get
请求中的路径–因为它不是以斜杠开头的,所以它被视为相对于当前文档路径

如果您的端点位于
/api/v1/users/current
,则应在请求中指定该路径:

return axios.get('/api/v1/users/current', config)
  .then(...
你说得对。在路径开头添加“/”解决了问题。非常感谢你,伙计!
return axios.get('/api/v1/users/current', config)
  .then(...