0
votes

I've got 2 domains - domain1.com and domain2.com - where domain1.com is my parent site and domain2.com is the child site. I'm setting cookies on domain1.com, but domain2.com needs access to those cookies.

I've researched a ton about this subject - JSONP, CORS, pick anything. EasyXDM seemed to offer a great solution. The messaging between domain2.com and domain1.com works as expected, but certain browsers, namely Chrome and IE, are not reading the cookie and therefore passing blank data.

Domain1.com has a page called status.php with the following data:

<?php
    $guid = $_COOKIE['user_guid'];
?>

<!doctype html>
<html>
    <head>
        <title>easyXDM.Transport test</title>
        <script type="text/javascript" src="/js/easyxdm/easyXDM.debug.js"></script>
    </head>
    <body>
        <script>
        var socket = new easyXDM.Socket({
            onReady: function() {
                socket.postMessage('<?php echo $guid; ?>');
            }
        });
        </script>
    </body>
</html>

Domain2.com has the following setup to receive the message from domain1.com:

var socket = new easyXDM.Socket({
    remote: "http://domain1.com/status.php",
    onMessage: function(message, origin)
    {
        alert("Received '" + message + "' from '" + origin + "'");
    }       
});

This works like a champ in FireFox, but Chrome and IE are returning an empty string, not getting the cookie data. Can anyone nudge me in the right direction with this? I can't set cookies on both domains, I just need to be able to grab the ID from the first one by any means necessary. Thanks!

As requested in the comments, here's the full code for Domain 2:

<!doctype html>
<html>
    <head>
        <title>Domain 2</title>
        <script type="text/javascript" src="/js/easyxdm/easyXDM.debug.js"></script>
    </head>
    <body>
        <script>
        var socket = new easyXDM.Socket({
            remote: "http://domain1.com/status.php",
            onMessage: function(message, origin)
            {
                alert("Received '" + message + "' from '" + origin + "'");
            }       
        });
        </script>
        <p>Hello World!</p>
    </body>
</html>
1
you don't need cookies, you need postMessage(). or even if you need cookies, you still need postMessage()... if you need IE6+7 support (gasp), you can use the window.name trick.dandavis
@dandavis - I've tried postMessage(). It suffers from the same issue. It's not passing the data from the cookie.Dexter
ok, but that has nothing to do with JS as all JS sees is strings...dandavis
I'm passing the cookie data as a string to JS to pass back to the domain2.com that needs it. It's not getting the cookie data. My understanding from my reading about this topic is that it's a cross-domain issue. I'm trying to figure out how to solve that using any method necessary.Dexter
if hard-coded string value work where cookies don't, then it's a php/broswer policy issue, not a js one.dandavis

1 Answers

0
votes

Solved this with xdomain instead: https://github.com/jpillora/xdomain

Easy to implement and solved the problem of cross-domain data. Far from perfect, but until CORS is better supported and older browsers drop off, it worked.