Cross domain issue with iframes

I know solutions for domain calls in a cross browser. Use JSONP, proxy call or accept domains on the server. I found another weird way today at my company.

Method:

They change the host to match the host of the second server using this -

window.location.host = "xyz.com";
          or
document.domain = "xyz.com";

Then they create a hidden iframe and get the content in the iframe and replace the content with the visible element.

Problem:

It works with iframe, but if I make an ajax call, it does not work. Any words on this?

+5
source share
1 answer

I'm not a jsonp fan, it creates a connection between data and presentation, and therefore I have explored this problem before, and well, here is a trick you can use, follow this:

, A "" iframe B. A B , , :

A sub1.example.com

B sub2.example.com

, , , , , , :

document.domain = "example.com";

B ajax (sub2.example.com), , A, . B, , , A B , .

, B , . , , , , .

- , , , , .

-:

in A
==================
document.domain = "example.com";
var child; // keep reference to B
function setChild(win) {
    childDocument = win;
}

function handleMessage(message) {
    do what ever it is you need to
}

in B
==================
make ajax request
document.domain = "example.com";
parent.setChild(this);

function ajaxCallback(message) {
    parent.handleMessage(message);
}
+3

All Articles