window.opener使用javascript进行跨域调用

window.opener使用javascript进行跨域调用,javascript,cross-domain,Javascript,Cross Domain,我有一个类似123.example.com的子域url,我正在使用Google或Facebook进行Oauth登录。现在的过程就像当我使用Google或Facebook单击登录时,会弹出一个窗口并获取详细信息,在获取详细信息后,我想在父窗口中显示详细信息。所以我使用window.opener.$(“#first_name”).val(firstName);,我遇到了一个类似这样的错误:访问属性“$”的权限被拒绝。 如果不是子域,则其工作正常。如何获取主窗口中的值 您有两个选择: document

我有一个类似123.example.com的子域url,我正在使用Google或Facebook进行Oauth登录。现在的过程就像当我使用Google或Facebook单击登录时,会弹出一个窗口并获取详细信息,在获取详细信息后,我想在父窗口中显示详细信息。所以我使用window.opener.$(“#first_name”).val(firstName);,我遇到了一个类似这样的错误:访问属性“$”的权限被拒绝。 如果不是子域,则其工作正常。如何获取主窗口中的值

您有两个选择:

document.domain
如果两个窗口位于同一根域中,并且问题是子域,则可以使用解决问题,例如,在
123.example.com
上的窗口中,可以执行以下操作:

document.domain = "example.com";
…允许它在
example.com上与windows对话。

网络信息 更通用(和现代)的解决方案是,允许在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);
// 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");
http://child-origin/child.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);
// 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");
根据您打开子窗口的方式,您可以消除子窗口向父窗口询问信息的部分(例如,如果父窗口有办法知道子窗口已完全加载并准备好接收消息)。不过,让孩子提问是确保他准备好接收信息的一个很好的方法


最初,web消息只允许传递字符串,但现代浏览器允许传递被克隆的对象。还请注意,如果您有
canvas
对象或类似对象,则可以在
postMessage
的第三个参数中传递它们,这样它们就会被传输:发送方不再有权访问它们,只有接收方可以访问它们。这让我们避免了复制大型内容(如果可能),同时也避免了多个线程同时访问同一数据的问题。

谢谢,但这不支持早期浏览器,因为我使用的是google appengine,所以我将使用Channel Api。我得到了解决方案,google和Facebook重定向url有一个状态参数,这样我们就可以发送和接收数据,并跨域发送数据。