簡體   English   中英

交叉請求時出錯:“ Access-Control-Allow-Origin不允許原產”?

[英]Error on cross request: “Origin is not allowed by Access-Control-Allow-Origin”?

我正在嘗試從另一個網站加載內容:

<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內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM