2013-02-08 146 views
6

我想通过xhr得到一个http:// javascript文件,但我遇到了上面提到的错误。XMLHttpRequest无法加载原产地不允许访问控制允许来源

这里是我的代码:

function getXHR() { 
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; 

    if (is_chrome) { 
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", "http://api.widgets.org/widget/1.1.2/widget_api.js?autoCreate=false&log=true", true); 
    xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4) { 
     var s = document.createElement('script'); 
     s.textContent = xhr.responseText; 
     (document.head||document.documentElement).appendChild(s); 
     s.parentNode.removeChild(s); 
     } 
    } 
    xhr.send(); 
    } 
} 

这仅适用于Chrome浏览器,因为我想使用HTTPS的脚本://而Chrome会自动阻止任何的http://。从中获得脚本的服务器不运行https://并且我需要脚本/有多个脚本,我宁愿不要将它们全部复制到数据文件中。

我遇到的错误:

XMLHttpRequest cannot load http://api.widgets.org/widget/1.1.2/widget_api.js?autoCreate=false&log=true. Origin https://mysite.com is not allowed by Access-Control-Allow-Origin. 
+0

[同源策略](https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript) – epascarello 2013-02-08 17:32:24

回答

4

只需插入<script>标签,而不是直接的这个XHR包装,然后插入内容到<script>标签。

function getScript() { 
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1; 

    if (is_chrome) { 
     // generate script element and set its source 
     var s = document.createElement('script'); 
     s.src = "http://api.widgets.org/widget/1.1.2/widget_api.js?autoCreate=false&log=true"; 
     // remove the script element after loading 
     s.addEventListener('load', function(){ s.parentNode.removeChild(s); }); 
     (document.head||document.documentElement).appendChild(s); 
    } 
} 

此外,我不知道,为什么你试图删除加载后的脚本元素。这不会影响在该代码中创建的任何对象/方法/变量。

+1

这是正确的。如果域名不同,则不能通过XHR执行此操作。阅读:https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=zh-CN&redirectslug=HTTP_access_control – marekful 2013-02-08 17:04:46

+0

此选项不完全解决http://和https://问题我有。 (ie [阻止]在https://mysite.com的页面运行不安全的内容从http://api.widgets.org/widget/1.1.2/widget_api.js?autoCreate=false&log=true) – user11235 2013-02-08 17:14:17

+0

我不是完全可以肯定,但我认为这个问题与http vs https没有任何关系,这是一个跨域资源问题,无论资源是http还是https,它都会被阻止。 – speakingcode 2013-02-08 17:29:14

1

为了与跨站点脚本相关的安全目的,浏览器的阻止XHR向服务器发出的XHR请求,该服务器与发出请求的页面的服务器不同。

如果只是要加载脚本,使用

<script src="..."></script> 

对于一般XHR,你可以使用JSONP解决方法,如果API提供,或询问API的运营商,使CORS(跨域资源共享)

http://developer.chrome.com/extensions/xhr.html https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS http://www.w3.org/TR/cors/ http://en.wikipedia.org/wiki/JSONP

3

我改为福将服务器文件的路径设置为短路径,如下所示。

$.post('http://example.com/pages/loadRandomImages.php',{'type':'loadRandomImages','loadingReq':'True'},function(data){ 

------------ 
---------- 
}); 

把它改成,

$.post('/pages/loadRandomImages.php',{'type':'loadRandomImages','loadingReq':'True'},function(data){ 

------------ 
---------- 
}); 

然后在铬工作得很好。

+0

只想说这个答案感谢!我有一个XMLHttpRequest.open()语句,因为上面的跨域错误而无法正常工作,并且通过使用相对路径,我使Chrome能够完美工作。非常感谢。 – AlexScript 2013-08-08 12:55:38

+0

不客气,感谢您的评论! – 2013-08-16 03:26:50

相关问题