小编典典

我如何在React.js中获取HTTP标头

reactjs

我已经向React页面发出了请求,我需要从请求中获取标头。

我通过URL调用页面:

http:// localhost / dashboard

然后设置标头,例如authcode = 1234。

然后,我使用以下路线加载页面:

<Route path="dashboard" name="dashboard" component={Dashboard} onEnter={requireAuth}></Route>

是否有类似this.props.header的东西,我可以在页面构造函数中调用?


阅读 327

收藏
2020-07-22

共1个答案

小编典典

您无法通过javascript发送http请求而无法获取当前页面标题。有关更多信息,请参见此答案。

在您的服务器上添加一个虚拟的api网址,并在页面加载后点击它,然后就可以获取标题了。

class App extends React.Component{
    //some code
    componentDidMount(){
       fetch(Some_API).then(response=>{
           console.log(response.headers)
       })
    }
    //some code
}
2020-07-22