AngularJs模板URL负载抛出CORS错误 [英] AngularJs template url loading throws CORS error
问题描述
我已经开发使用AngularJS的应用程序。
I have developed an app using AngularJS.
这是活在以下网址,但它不是因为装载模板CORS问题的工作:
http://www.ebust.it/app/#/lines/list
It is live at the following url but it isn't working because of CORS issue on loading templates: http://www.ebust.it/app/#/lines/list
该应用程序工作正常的网址没有www的:
http://ebust.it/app/#/lines/list
The app works fine on the url without "www": http://ebust.it/app/#/lines/list
这是我从JS控制台得到的错误:
The error that I get from js console:
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。你有没有遇到类似的东西?我不明白为什么角使用了错误域执行模板URL AJAX请求。
I also tried to force the template as absolute URL. Have you ever experiences something similar? I can't understand why Angular is using the wrong domain to perform the AJAX request for the template URL.
推荐答案
的 $ HTTP
Ajax请求成为当脚本请求重定向跨域:
The $http
Ajax requests become cross-origin when requests for the scripts are redirected:
<script src="/themes/frontend/spa/app/scripts/controllers/line.js"></script>
curl -v http://www.ebust.it/themes/frontend/spa/app/scripts/services/line.js
...
< HTTP/1.1 301 Moved Permanently
...
< Location: http://ebust.it/themes/frontend/spa/app/scripts/services/line.js
...
每个脚本绑定到的是,它提供的由来, http://ebust.it/
,而不是文件, http://www.ebust.it/
。
Each script is tied to is the origin that serves it, http://ebust.it/
, rather than that of the document, http://www.ebust.it/
.
要解决这个问题,你可以:
To resolve this, you could:
-
即成来自起源脚本,而不是重定向。
Serve the scripts from both origins rather than redirecting.
指定在AJAX网址提供脚本的主机名:
Specify the hostname that serves the scripts in the Ajax URLs:
return $http({
// ...
url: '//ebust.it/line/georeverse'
});
调整原点与 document.domain的
。
document.domain = 'ebust.it';
url: '//' + document.domain + '/line/georeverse'
或配置CORS允许2原点的请求。
Or configure CORS to allow the requests between the 2 origins.
这篇关于AngularJs模板URL负载抛出CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!