【问题标题】:window.opener return Cross Origin error in Chromewindow.opener 在 Chrome 中返回 Cross Origin 错误
【发布时间】:2020-02-27 21:06:24
【问题描述】:

我已经开发了应用程序来使用下面的代码打开弹出窗口,

var url = 'child.html';
var args = ['value'];
var options='height:150px;width:300px'
window.openModalDialog(url,args,options);

我可以使用下面的代码从 openModalDialog 读取参数

var args = window.dialogArguments;
var arg = args[0];

现在正在将应用程序迁移到 Chrome。根据文档 window.openModalDialog Chrome 不支持。 所以,我打算用 window.open 代替它。当我试图争论时,现在正面临问题。 因为,我无法使用 window.dialogArguments 获得论据。 我试过了

window.opener and parent.window.opener

获得论据。它返回跨域错误。 如何从 window.open 获取参数?

【问题讨论】:

    标签: javascript google-chrome cross-domain window.open window.opener


    【解决方案1】:

    使用window.open,您应该将开启程序保存到变量中,然后您可以访问参数。

    var url = 'child.html';
    var args = ['value'];
    var options='height:150px;width:300px'
    let newWindow = window.open(url,args,options);
    

    参数将在newWindow.location 对象内。

    注意:如果您的窗口在不同的域中打开,这将不起作用,因为这会试图破坏名为 CORS 的安全策略。

    【讨论】:

    • 谢谢@k3llydev。正在同源打开窗口,父窗口 url 是 localhost:8080/project/result 子窗口 - window.open url 是 localhost:8080/project/popup. 仍然出现 CROS 错误,如何确保我在同一个域中打开窗口。?
    • 你在同一个来源,即localhost。尝试在 localhost:8080/project/popup 路径中返回以下标头:Access-Control-Allow-Origin: *。最近,您可以将 * 替换为您将在生产中使用的域:Access-Control-Allow-Origin: example.com
    猜你喜欢
    • 2015-05-22
    • 2021-08-06
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2020-09-17
    • 2015-06-12
    相关资源
    最近更新 更多