简体   繁体   English

同源政策

[英]Same-origin policy

I'm getting this error while calling javascipt: 调用javascipt时出现此错误:

XMLHttpRequest cannot load https://sub.domain.com/actions/action1.php . XMLHttpRequest无法加载https://sub.domain.com/actions/action1.php No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' https://www.domain.com ' is therefore not allowed access. 因此,不允许访问来源“ https://www.domain.com ”。

  • Javascript is placed here: https://www.domain.com/page.php Javascript放在这里: https://www.domain.com/page.php : https://www.domain.com/page.php
  • Javascript do job through: https://sub.domain.com/actions/action1.php (requested resource) Javascript通过以下方式完成工作: https://sub.domain.com/actions/action1.phphttps://sub.domain.com/actions/action1.php (请求的资源)

I have found the easiest way how to solve this problem (same-origin policy): 我找到了解决此问题的最简单方法(同源政策):

<script type="text/javascript">
document.domain = 'domain.com';
</script>

But this isn't solving my problem. 但这不能解决我的问题。 I have also tried to add this piece of code to both pages, but still nothing. 我也曾尝试将这段代码添加到两个页面中,但仍然一无所获。

Any simple suggestions how to solve it? 有什么简单的建议怎么解决呢?

As stated in the error, you will need to add the Access-Control-Allow-Origin to the headers of the response, or use a different method such as JSONP . 如错误所述,您需要Access-Control-Allow-Origin到响应的标头中,或使用其他方法(如JSONP) This is a JavaScript security feature to prevent cross site scripting and cannot be circumvented. 这是一种JavaScript安全功能,可防止跨站点脚本编写并且无法规避。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM