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

问题描述:

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

I've made a request to a React page, and I need to get the headers from the request.

我通过URL呼叫页面:

I call the page via the URL:

http://localhost/dashboard

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

and I set headers, for example authcode=1234.

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

I then load the page with this route:

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

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

is there something like this.props.header, I can call in the page constructor?

如果不通过javascript发送http请求,则无法获取当前页面标题. 有关更多信息.

You cant get current page headers without sending a http request via javascript. See this answer for more info.

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

Add a dummy api url on your server and hit it after your page loadn then you can get the headers.

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