【发布时间】:2020-04-30 18:21:10
【问题描述】:
我想从一个 React 应用程序请求另一个 React 应用程序,然后将其加载到新选项卡中。该请求包含授权标头,因此该要求阻止我使用常规静态链接或window.open(link, '_blank');,我需要使用适当的请求。我正在做的事情相当简单:
const handleClick = jwt => {
fetch(url,
{
method: 'GET',
headers: {
'Accept': 'text/html',
'Authorization': 'Bearer ' + jwt
}
})
.then(response => response.text())
.then(body => {
const w = window.open("");
w.document.open();
w.document.write(body);
w.document.close();
});
};
该代码会打开一个新选项卡并注入 HTML。然而,接下来发生的事情是加载的 index.html 加载了原始打开应用程序的 React 文件,而不是打开的应用程序,并且新选项卡中的结果只是原始应用程序。索引包含的脚本标签对于两个应用程序都是相同的,至少在开发中:
<script src="/static/js/bundle.js"></script>
<script src="/static/js/1.chunk.js"></script>
<script src="/static/js/main.chunk.js"></script>
<script src="/main.4bf234700064bd70611e.hot-update.js"></script> //this line differs ofc, but it is not important for the purpose of the question
有没有办法强制从 localhost:8000(我要渲染的应用程序)而不是 localhost:4000(我的原始应用程序运行的地方)下载这些新文件?
【问题讨论】:
标签: javascript reactjs browser xmlhttprequest