Javascript 跨域 - “允许”其他域?
步骤说明
- 在我的域中的自定义页面上编写“回调”函数(为了便于论证,称为“MyCallbackCallback”)
- 打开新窗口(来自不同的域)并将函数名称作为查询字符串
- 新窗口执行其需要执行的操作,然后尝试从我的自定义页面访问“MyCallback”,
这显然不会工作,并将返回“访问被拒绝”错误。
当然,如果有一种方法“允许”第三方域访问我的域,就可以解决问题。有这样的事吗?我知道 Action Script 中有,但是 JavaScript ?
注意 - 我知道在两个页面上设置“document.domain”(或在同一域中创建两个页面)可以解决问题,但我几乎肯定不会有这个选项。
如果答案是“你不能”,那也没关系——我只需要知道。我花了很多时间搜索,但找不到一个简单的答案(可能没有一个!)
Ta,Rob
Description of steps
- Write "callback" function on my custom page in my domain (called "MyCallbackCallback", for the sake of argument)
- Open new window (from different domain) and pass function name in as part of query string
- New window does what it needs to do then tries to access "MyCallback" from my custom page
This obviously won't work and will return "Access denied" error.
If there was a way of "allowing" the 3rd-party domain access to my domain that would solve the issue, of course. Is there such a thing? I know there is in Action Script, but JavaScript??
NB - I am aware that setting "document.domain" on both pages, (or creating both pages in the same domain) will solve the issue, but I almost certainly won't have this option.
If the answer is "you can't" that's fine - I just need to know. I have spent many hours searching and can't find a simple answer (there may not be one!)
Ta, Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的问题中尚不清楚,但如果您尝试使用 CORS,服务器您请求的数据应添加一个
Access-Control-Allow-Origin
HTTP 标头,如下所示:或者,如果它是公共资源:
旧版浏览器不支持 CORS。如果您需要完全跨浏览器兼容的解决方案,请使用 JSONP。
It’s not exactly clear from your question, but if you’re trying to use CORS, the server you’re requesting data from should add an
Access-Control-Allow-Origin
HTTP header, like so:Or, if it’s a public resource:
Older browsers don’t support CORS. If you need a fully cross-browser-compatible solution, use JSONP.
查看跨域 AJAX 请求:
JSONP 是唯一与旧版浏览器兼容的方法。
Have a look at Cross-Domain AJAX requests:
JSONP is the only method compatible with older browsers though.
如果您想要在没有服务器端代理的情况下进行跨域通信(非常适合您所描述的 RPC 类型),请查看 easyXDM。
您可以此处找到多个演示。
If you want cross-domain communication without serverside proxies (perfect for the kind of RPC that you are describing) then take a look at easyXDM.
You can find multiple demos here.