【问题标题】:window.opener cross domain call using javascriptwindow.opener 使用 javascript 进行跨域调用
【发布时间】:2016-08-24 15:04:08
【问题描述】:

我有一个像 123.example.com 这样的子域 url,因为我正在使用 Google 或 Facebook 进行 Oauth 登录。现在的过程就像当我点击使用 google 或 facebook 登录时,会弹出一个窗口并将获取详细信息,获取详细信息后我想在父窗口中显示详细信息。所以我正在使用 window.opener.$("#first_name").val(firstName);,我收到类似这样的错误 Error: Permission denied to access property '$'。 如果它不是子域,它的工作正常。如何将值获取到主窗口。

【问题讨论】:

    标签: javascript cross-domain


    【解决方案1】:

    两种选择:

    document.domain

    如果两个窗口都在同一个根域中并且问题是子域,您也许可以使用document.domain property 来解决它,例如在123.example.com 上的窗口中,您可以这样做:

    document.domain = "example.com";
    

    ...允许它与example.com.上的窗口对话

    网络消息

    更通用(和现代)的解决方案是web messaging,它允许在 SOP 通常会阻止直接通信的地方进行协作跨域通信。所以我们有http://parent-origin/parent.html,它想要打开并与http://child-origin/child.html 通信。在http://parent-origin/parent.html

    // Listen for messages
    window.addEventListener("message", function(e) {
        // If we get a message from the child, and the message is asking for
        // us to send the info...
        if (e.origin === "http://child-origin" && e.data === "send-info") {
            // ...send it
            log("Got request from child, sending info");
            e.source.postMessage({command: "info", info: { /*...data goes here...*/ }}, e.origin);
        }
    }, false);
    

    http://child-origin/child.html:

    // Listen for messages
    window.addEventListener("message", function(e) {
        var info;
    
        // If the message is from the parent and it says it has the info...
        if (e.origin === "http://parent-origin" && e.data && e.data.command === "info") {
            // Use info
            info = e.data.info;
            log("Info is " + JSON.stringify(info));
        }
    }, false);
    
    // Ask the opener to send us the info
    opener.postMessage("send-info", "http://parent-origin");
    

    根据您打开孩子的方式(例如,如果父窗口有办法知道孩子已满载并准备好),您也许可以消除孩子向父母询问信息的部分消息)。不过,让孩子询问是确保它准备好接收信息的好方法。

    最初,网络消息只允许传递字符串,但现代浏览器允许传递被克隆的对象。另请注意,如果您有canvas 对象或类似对象,您可以将它们传递到postMessage 的第三个参数中,以便它们被转移:发送者不再有权访问它们,只有接收者。这让我们可以避免复制大的东西(在可能的情况下),同时也避免了多个线程同时访问相同数据的问题。

    【讨论】:

    • 谢谢,但这不支持早期的浏览器,因为我使用的是谷歌应用引擎,所以我将使用 Channel Api。
    • 我得到了解决方案,Google 和 Facebook 重定向 url 有一个状态参数,因为我们可以发送和接收数据,事件跨域。
    猜你喜欢
    • 2011-04-15
    • 1970-01-01
    • 2011-02-02
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多