交叉请求时出错:“访问控制允许源不允许源”

Error on cross request: "Origin is not allowed by Access-Control-Allow-Origin"?

本文关键字:许源 不允许 访问控制 请求 出错      更新时间:2023-09-26

我正在尝试从另一个站点加载内容:

<div id='include-from-outside'></div>
<script type='text/javascript'>
  $('#include-from-outside').load('http://lujanventas.com/plugins/banner/index.php&callback=?');
</script> 

但是我收到此错误:

XMLHttpRequest cannot load http://lujanventas.com/plugins/banner/index.php&callback=?. Origin http://lventas.com is not allowed by Access-Control-Allow-Origin.

我怎样才能防止它发生?

有两个选项:

1:使 http://lujanventas.com 返回正确的 CORS 标头 -- http://enable-cors.org/

2:使用服务器而不是浏览器中的JS请求html - http://www.daniweb.com/web-development/php/code/216729/php-proxy-solution-for-cross-domain-ajax-scripting

您使用的

网址表明该网站支持 JSONP(请参阅 http://en.wikipedia.org/wiki/JSONP)。如果是这样,您应该能够这样做:

<script type="text/javascript">
    function handleResponse(json){
       var data = JSON.parse(json);
       ...handle data...
    }
</script>
<script src="http://lujanventas.com/plugins/banner/index.php?callback=handleResponse"></script>
使用 jquery ajax 查询加载 lujaventas

内容的 php 文件,然后 ajax 回调将是 lujaventas 内容。