Javascript跨域 - “允许”其他域? [英] Javascript cross domain - "allow" other domains?

查看:154
本文介绍了Javascript跨域 - “允许”其他域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

步骤说明


  1. 在我的网域中的自订网页上写入「回呼」函式

  2. 打开新窗口(来自不同的域)并在查询字符串中传递函数名称

  3. 新建窗口会执行所需操作,然后尝试从自定义页面访问MyCallback

这显然 / strong>,并返回访问被拒绝错误。

This obviously won't work and will return "Access denied" error.

如果有一种方式允许第三方域访问我的域,问题,当然。有这样的事吗?我知道有在Action Script,但JavaScript?

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??

注意 - 我知道在两个页面上设置document.domain(或在同一个域中创建两个页面)会解决这个问题, 不会有此选项。

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

推荐答案

您的问题不完全清楚,但如果您尝试使用 CORS ,您要请求数据的服务器应添加 Access-Control-Allow-Origin HTTP标头,如下所示:

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:

Access-Control-Allow-Origin: http://example.org/

或者,如果它是公共资源:

Or, if it’s a public resource:

Access-Control-Allow-Origin: *

旧版浏览器不支持CORS。如果您需要完全跨浏览器兼容的解决方案,请使用 JSONP

Older browsers don’t support CORS. If you need a fully cross-browser-compatible solution, use JSONP.

这篇关于Javascript跨域 - “允许”其他域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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