【发布时间】:2021-02-11 20:00:02
【问题描述】:
我想在我的网页中嵌入一个网页。将嵌入的页面需要令牌进行身份验证。我不想通过查询参数发送令牌,而是在授权标头中发送它。我按照以下代码中的指定修改了 iframe 填充。
import React, { Component } from 'react'
import axios from 'axios'
export default class CustomService extends Component {
constructor(props) {
super(props);
this.theFrame = React.createRef();
}
render() {
const token = localStorage.getItem("token"),
header = { 'Authorization': 'Bearer ' + token },
url = "http://localhost:4000";
axios.get(url, { headers: header })
.then((response) => {
let doc = this.theFrame.current.contentDocument;
doc.open();
doc.write(response.data);
doc.close();
})
.catch((error) => {
console.log(error);
});
return <div>
<div>
<iframe style={{ width: "100vw", height: "50vh" }} ref={this.theFrame} title="headers set manually for this iframe"></iframe>
</div>
</div>
}
}
当代码执行时,我在控制台上收到以下错误。即使 iframe 的内容是正确的,也没有应用样式。
任何帮助表示赞赏,在此先感谢。
【问题讨论】: