Uj *_*orb -1 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={routeProps => (
<MyAccountPage {...routeProps} currentUser={this.state} />
)}
/>
[...]
Run Code Online (Sandbox Code Playgroud)
这是我使用 axios 的函数:
getCurrentUser: jwt => {
let config = {
headers: {}
}
if (jwt) {
config['headers']['Authorization'] = 'Bearer ' + jwt
}
return axios.get('api/v1/users/current', config)
.then(response => {
console.log(response)
return response.data
})
.catch(error => {
if( error.response ){
console.log(error: error.response.data);
}
return undefined
})
}
Run Code Online (Sandbox Code Playgroud)
/它与和路线完美配合/meals。但是,当 axios 被调用时,/user/:token它会尝试/user/api/v1/users/current从而不是/api/v1/users/current.
我怎样才能改变它,以便它适用于开发和生产环境?
问题出在请求 \xe2\x80\x93 中的路径axios.get,因为它不以斜杠开头,它被视为相对于当前文档路径。
如果您的端点位于/api/v1/users/current,那么这就是您应该在请求中指定的路径:
return axios.get('/api/v1/users/current', config)\n .then(...\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1530 次 |
| 最近记录: |