如何解决这个奇怪的跨域问题?
基本上,我的应用程序位于一个社交网络中,在我的应用程序页面中,他们创建了一个 iframe,其(不是我的)URL 为“渲染器”,然后它采用(不要问如何)我的代码(html, js)并放置在此 iframe 的 body
标记中。
由于我需要能够运行 iframe 的 JavaScript 函数,因此我决定不使用我的应用程序 URL 创建另一个 iframe,而是使用 ajax 调用将我的应用程序的内容加载到其 iframe 的主体中。这样我就可以运行他们的 JavaScript 函数。如果我在他们的 iframe 中创建我的 iframe,那么由于跨域的原因我无法运行它们,对吧?
然而,当我使用 jQuery 对我的应用程序执行 ajax 调用时,它们是从社交网络执行的(因为 iframe 是它们的,只是我的正文代码),因此保存在我的应用程序域上的会话 cookie 不可用于来自此的 ajax 调用iframe。
我认为我需要在社交网络的 iframe 中创建一个 iframe (dooh),但是如何克服跨域问题来访问父 iframe 中的 JavaScript 函数?
PS 抱歉,解释很长。想给大家说清楚。
Basically my application is in a social network where in my application's page they create an iframe with their (not mine) URL to "renderer" which then takes (don't ask how) my code (html, js) and places in the body
tag of this iframe.
Since I need to be able to run the iframe's JavaScript functions I decided not to create an another iframe with my application URL, but with ajax calls just load my application's content in the body of their iframe. This way I could be able to run their JavaScript functions. If I would create my iframe within their iframe then I couldn't run them because of the cross domain stuff, right?
However when I perform ajax calls with jQuery to my application they are performed from the social network (since the iframe is their, just my body code) and thus no session cookies which are saved on my application domain are available for the ajax calls from this iframe.
What I think is I need to create an iframe (dooh) within the social network's iframe, but how to overcome the cross domain issues to access the JavaScript functions in the parent iframe?
P.S. Sorry for the long explanation. Wanted to make it clear for everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法从另一个域读取 cookie。
AJAX 失败是因为您使用的是 XHR 并且您被同源策略阻止。或者,您正在使用 JSONP,并且未设置 cookie。
如果您使用的是 XHR,请切换到 JSONP。
使用 JSONP 您将无法设置 cookie。 JSONP只是通过设置脚本标签的src来加载脚本,而cookie不能以这种方式设置(没关系,它们不能从另一个域设置)。
您必须通过在每个 JSONP 请求中传递会话 ID 来手动管理状态。
There is no way to read a cookie from another domain.
Either the AJAX is failing because you are using XHR and you are getting blocked by the Same Origin Policy. Or, you are using JSONP, and the cookie is not being set.
If you are using XHR, switch to JSONP.
Using JSONP you won't be able to set cookies. JSONP just loads a script by setting the src of a script tag, and cookies can't be set in this way (nevermind that they can't be set from another domain).
You'll have to manage state manually by passing the session id with each JSONP request.