0

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>
Dexter
  • 1,128
  • 3
  • 25
  • 51
  • 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 Feb 13 '14 at 21:48
  • @dandavis - I've tried postMessage(). It suffers from the same issue. It's not passing the data from the cookie. – Dexter Feb 13 '14 at 21:49
  • ok, but that has nothing to do with JS as all JS sees is strings... – dandavis Feb 13 '14 at 21:51
  • 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 Feb 13 '14 at 21:55
  • if hard-coded string value work where cookies don't, then it's a php/broswer policy issue, not a js one. – dandavis Feb 13 '14 at 21:57
  • @dandavis - I'm sorry, don't mean to be rude, but where am I defining it as solely a JavaScript issue? It's an issue that I need help with and I'm trying to provide as much information as possible so someone who's come across it may be able to point me in the right direction. Why does it matter whether it's a JS issue or not? – Dexter Feb 13 '14 at 21:58
  • not to be rude, but you are defining it as such in your question's tags, which all relate to JS concepts. – dandavis Feb 13 '14 at 22:00
  • Because I'm using JavaScript as part of the solution. – Dexter Feb 13 '14 at 22:01
  • well, do what you want, but i'd expect you to get more and better help with a php tag and framing the question as "why isn't PHP seeing my cookie?" so that you can fix the actual problem... – dandavis Feb 13 '14 at 22:05
  • PHP is reading the cookie, when I access it directly. It's only when I make the request for the PHP page via JavaScript that it's not seeing it. – Dexter Feb 13 '14 at 22:06
  • then post the whole code of the two pages; i see no JS url loading here. I'm trying to help i swear... – dandavis Feb 13 '14 at 22:08
  • I updated it to include code for the full domain 2, but it really is a very simple implementation. Domain 2 is using easydxdm to accept a message from http://domain1.com/status.php. The status.php page on Domain 1, on load, executes this line: socket.postMessage(''); I can confirm that's working, Domain 2 does receive the message. The issue is that in Chrome and IE, even when $_COOKIE['user_guid'] does contain valid data, it returns a blank string. I need to figure out how to get the string GUID from that cookie posted to Domain 2 by any means necessary. Thanks. – Dexter Feb 13 '14 at 22:14
  • maybe i'm just stupid, but i don't see any connection between the two pages (iframe, popup, etc). the easyXDM page has broken links to relevant examples. seems like a lot to tack on and debug just to support IE7... – dandavis Feb 13 '14 at 22:21

1 Answers1

0

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.

Dexter
  • 1,128
  • 3
  • 25
  • 51
  • there is so few people that have worked with easyXDM. May you look at http://stackoverflow.com/questions/27203172/easyxdm-download-files-from-3rd-party-service please? – VB_ Nov 29 '14 at 22:31