I thought cross-subdomain AJAX requests were allowed, but this Chrome error seems to indicate otherwise

淺唱寂寞╮ 提交于 2019-12-17 18:36:07

问题


I know that cross-domain requests are disallowed for security reasons, but I was under the impression that only the top-level domain needed to match, that different sub-domains were okay. However, I am getting this error from Chrome 7:

"Unsafe JavaScript attempt to access frame with URL http://foo.somedomain.com/dir/page.html from frame with URL http://bar.somedomain.com/otherdir/otherpage.html. Domains, protocols and ports must match"

What exactly are the rules for these types of requests?


回答1:


In short, the rules of the same origin policy are:

  • same host
  • same port
  • same protocol

In your example you are violating the host rule, as a different subdomain could point to a different host/ IP than another, even if the second level domain is the same.

If you have no other possibility, you could try to use JSONP in your ajax request; this doesn't have an SOP.

Reference




回答2:


No cross sub domain requests are not allowed in any browser. But there are some ways like CORS, using iframes, setting document.domain to make it work (although with some limitations).



来源:https://stackoverflow.com/questions/4025061/i-thought-cross-subdomain-ajax-requests-were-allowed-but-this-chrome-error-seem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!