Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs模板url加载引发CORS错误_Javascript_Angularjs_Cors - Fatal编程技术网

Javascript AngularJs模板url加载引发CORS错误

Javascript AngularJs模板url加载引发CORS错误,javascript,angularjs,cors,Javascript,Angularjs,Cors,我用AngularJS开发了一个应用程序 它在以下url处处于活动状态,但由于加载模板时出现CORS问题而无法工作: 该应用程序在没有“www”的url上运行良好: 我从js控制台得到的错误: XMLHttpRequest cannot load http://ebust.it/themes/frontend/spa/app/views/lines.html. No 'Access-Control-Allow-Origin' header is present on the requeste

我用AngularJS开发了一个应用程序

它在以下url处处于活动状态,但由于加载模板时出现CORS问题而无法工作:

该应用程序在没有“www”的url上运行良好:

我从js控制台得到的错误:

XMLHttpRequest cannot load http://ebust.it/themes/frontend/spa/app/views/lines.html. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.ebust.it' is therefore not allowed access.

我还试图强制将模板作为绝对URL。你有过类似的经历吗?我无法理解为什么Angular使用了错误的域来执行模板URL的AJAX请求。

您可以使用
window.location.hostname
或Angular方式
$location.host()
来获取适当的“www.ebust.it”或“ebust.it”并通过连接到主机的路径来加载模板。

您是否尝试过相对URL而不是绝对URL?你用什么来路由?用户界面路由器?理想情况下,如果您使用的是ngRouter或ui路由器,如果您提供了相对路径,这将不会导致CORS问题。只有当您提供绝对(且不正确,即属于不同域)URL时,CORS才会出现。重定向脚本请求时,
$http
Ajax请求会变成跨源请求:

<script src="/themes/frontend/spa/app/scripts/controllers/line.js"></script>
  • 调整原点,使用

  • 或者配置CORS以允许两个来源之间的请求


  • 也许吧。但是,
    ebust.it
    www.ebust.it
    是不同的主机名和名称。所以,他们天生就没有访问对方客户端的权限。你介意提供一些相关的角度代码吗?我同意你的看法。我需要AngularJS使用与浏览器窗口相同的主机。它不工作。我已经试过这种方法了。AngularJS继续使用ebust.it作为主机名而不是www.ebust.itThanks来执行请求。你的回答帮助我弄清楚发生了什么。我不敢相信Apache vhost上的一个简单别名会破坏AngularJS应用程序。这些步骤应该记录在某个地方。不管怎样,我必须把最后两颗子弹放在一起才能让它起作用。
    return $http({
        // ...
        url: '//ebust.it/line/georeverse'
    });
    
    document.domain = 'ebust.it';
    
    url: '//' + document.domain + '/line/georeverse'