浏览器/JavaScript同源策略如何适用于二级域名?
我有一些 JavaScript 在同一域上的两个独立服务器之间共享请求。
JavaScript 中的域是否需要 .com?
在这种情况下,两台服务器都位于 .abc.tyy 域上,tyy 通常是 .com
想知道我是否只能使用 .com 作为该域?我收到权限被拒绝错误,但此代码在同一域(.com)上的其他单独服务器上运行良好。
更新: 这正是我使用它的方式:
123.abc.tyy 有一个加载我想要访问的属性的脚本。
123.abc.tyy 上的脚本在打开脚本标记处将 document.domain 设置为“abc.tyy”。
当我从 234.abc.tyy 调用 123.abc.tyy 脚本中的“getUser()”函数时,出现权限被拒绝错误。
我调用“getUser()”的方式是: 我在浏览器中访问 http://123.abc.tyy,该网站允许我指定 URL加载到它的其中一个框架中。我将该 URL 指向 http://234.abc.tyy/BeginLoadPatient.aspx"该页面我正在执行以下操作:
window.location = 'http://234.abc。 tyy/LoadPatient.aspx?PatientId=' + getUser() '; getUser 是源自 123.abc.tyy 的函数
如果我将 234.abc.tyy 和 123.abc.tyy 添加到我的受信任站点,一切正常 - 这是跳过同源策略吗?
I have some JavaScript that is sharing a request between two separate servers on the same domain.
Is .com a requirement for the domain in JavaScript?
In this case both the servers are on the .abc.tyy domain with the tyy being what would normally be .com
Wondering if I can only use .com for the domain? I am getting a permission denied error, but this code works fine on other separate servers on the same domain(.com).
Updated:
Here is exactly how I'm using this:
123.abc.tyy has a script that loads properties that I want to access.
The script on 123.abc.tyy at opening script tag, sets the document.domain to 'abc.tyy'.
When I call the 'getUser()' function in 123.abc.tyy's script FROM 234.abc.tyy I am getting a permission denied error.
The way I am calling 'getUser()' is:
I access http://123.abc.tyy in a browser, and the site allows me to specify a URL to load in one of it's frames. I point that URL to http://234.abc.tyy/BeginLoadPatient.aspx" in that page I am doing the following:
window.location = 'http://234.abc.tyy/LoadPatient.aspx?PatientId=' + getUser() '; with getUser being a function originating in 123.abc.tyy
If I add 234.abc.tyy and 123.abc.tyy to my trusted sites, everything works fine - is this skipping over the same origin policy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,SOP 不关心域是什么,只关心它代表相同的起源。 (您是否在某处硬编码了 .com 域?)
请注意,需要考虑的不仅仅是域。 同源策略将协议、端口和主机视为出色地。因此,
aaa.abc.tyy
和bbb.abc.tyy
是不同的来源。如果您可以控制所涉及的服务器,则可以查看跨源资源共享,但不幸的是 CORS 仅在现代浏览器中实现(并且在支持它的 IE 版本上,仅当您显式使用它时才支持)。
当然,另一个选择是 JSON-P,它具有交叉工作的优点- 现在的浏览器。
另一件需要注意的是
document.domain
,详细信息此处< /a> 和此处。编辑后更新:
您还需要在
BeginLoadPatient.aspx
中将document.domain
设置为“abc.tyy”。我一点也不感到惊讶(尽管对我来说这会很狡猾),但我对此没有第一手的了解。会很容易测试。
No, the SOP doesn't care what the domain is, only that it represents the same origin. (Could it be that you have the .com domain hard-coded somewhere?)
Note that there's more than the domain to consider. The Same Origin Policy looks at protocol, port, and host as well. So
aaa.abc.tyy
andbbb.abc.tyy
are different origins.If you're in control of the servers involved, you might look at Cross-Origin Resource Sharing, but unfortunately CORS is only implemented in modern browsers (and on those versions of IE where it's supported, it's only supported if you use it explicitly).
Another option, of course, is JSON-P, which has the advantage of working cross-browser right now.
Another thing to look at is
document.domain
, details here and here.Update after your edits:
You'll need to set
document.domain
to "abc.tyy" inBeginLoadPatient.aspx
as well.I wouldn't be at all surprised (although to me it would be pretty dodgy), but have no first-hand knowledge of it. Would be easy to test.