在不同的域上设置 cookie,使用 javascript 或其他 [英] Getting setting cookies on different domains, with javascript or other

查看:57
本文介绍了在不同的域上设置 cookie,使用 javascript 或其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法在网上找到任何与这种情况有关的特定信息,所以我开始...我需要在浏览second.com"时设置/获取存储在first.com"中的 cookie,我可以完全访问first.com",但我只有 javascript 访问权限(可以根据需要操作 DOM)在第二个.com".

Haven't been able to find anything particular to this situation online so here i go... I need to set/get the cookies stored at "first.com" while browsing "second.com", I have full access of "first.com" but i only have javascript access (can manipulate the DOM as i want) on "second.com".

我的第一种方法是在 second.com(使用 js)上创建一个 iframe,它加载了一个页面,比如first.com/doAjax?setCookie=xxx",然后通过 ajax 调用说first.com/setCookie?cookieData=xxx",它将使用我们传递的数据在first.com"上设置 cookie.

My first approach was to create an iframe on second.com (with js) that loaded a page like "first.com/doAjax?setCookie=xxx" and that did an ajax call to say "first.com/setCookie?cookieData=xxx" which would set the cookie on "first.com" with the data we passed around.

这对于从 second.com 在 first.com 上设置 cookie 非常有效 - 为了获取 cookie,我基本上遵循了相同的过程,创建了加载first.com/doAjax?getCookie"的 iframe,然后就可以了一个 ajax 调用说first.com/getCookie",它将读取 first.com 上的 cookie 信息并将其作为 JSON 对象返回.

That pretty much worked fine for setting the cookie on first.com from second.com - for getting a cookie I basically followed the same procedure, created the iframe that loaded "first.com/doAjax?getCookie" and that would do an ajax call to say "first.com/getCookie" which would read the cookie info on first.com and return it as a JSON object.

问题是我无法将该 JSON cookie 对象带回second.com",因此我可以读取它,也许我可以在使用window.top"完成 Ajax 调用时将其带回,但是存在计时问题,因为它与 iframe 加载时间无关.我希望我很清楚,并且想知道是否有一个更简单的解决方案,而不是这个疯狂的 iframe->ajax 废话,这似乎也不适用于在 SAFARI 中获取 cookie.

The problem is that I'm unable to bring that JSON cookie object back to "second.com" so I can read it, well maybe i could just bring it when the Ajax call is complete using "window.top" but there's timing issues because its not relative to when the iframe has been loaded. I hope i am clear and was wondering if there's an easier solution rather than this crazy iframe->ajax crap, also seems like this wont even work for getting cookies in SAFARI.

推荐答案

您可以使用回调将脚本元素注入到文档的 HEAD 中,该回调将您需要的 cookie 传递给任何需要它的函数.

You could inject a script element into HEAD of the document with a callback that passes the cookie you need to whatever function needs it.

类似于:

 <script type="text/javascript">
   var newfile=document.createElement('script');
   newfile.setAttribute("type","text/javascript");
   newfile.setAttribute("src", 'http://first.com/doAjax?getCookie&callback=passCookie');
   document.getElementsByTagName("head")[0].appendChild(newfile);
 </script>

页面 first.com/doAjax?getCookie 可以这样做:

And the page first.com/doAjax?getCookie could do this:

     passCookie({'name':'mycookie', 'value':'myvalue'});

这篇关于在不同的域上设置 cookie,使用 javascript 或其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆