我如何在 React.js 中获取 http 标头 [英] How do I get http headers in React.js
问题描述
我已向 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:
并且我设置了标题,例如 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 url,并在您的页面加载后点击它,然后您就可以获得标题.
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
}
这篇关于我如何在 React.js 中获取 http 标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!