本文介绍了我如何在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:
然后设置标头,例如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
}
这篇关于我如何在React.js中获取HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!