[英]No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access.
I have a node.js server which is running locally on my computer. 我有一个node.js服务器,它在我的计算机上本地运行。 And I am developing html page with js-code: 我正在用js代码开发html页面:
var MainController = function($scope, $http) {
var onCountComplete = function(response) {
$scope.count = response.data;
};
var onError = function(reason) {
$scope.error = "Could not fetch data";
};
$http.get("http://localhost:1337/api/count")
.then(onCountComplete, onError);
$scope.message = "hello";
};
I get the error when I were running this code: 运行此代码时出现错误:
XMLHttpRequest cannot load
http://localhost:1337/api/count
. XMLHttpRequest无法加载http://localhost:1337/api/count
。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'file://' is therefore not allowed access. 因此,不允许访问源“ file://”。
I have tried to launch the Chrome with --allow-file-access-from-files key, but it doesn't helps. 我曾尝试使用--allow-file-access-from-files键启动Chrome,但这无济于事。 Is there a way to avoid this error during development? 有没有一种方法可以避免在开发过程中出现此错误?
If you're using Apache, you can try this solution. 如果您使用的是Apache,则可以尝试此解决方案。 This has worked for me on a few small projects. 这已经为我完成了一些小项目。 Just add these lines to your .htaccess file. 只需将这些行添加到您的.htaccess文件。 Customize as needed though, my example below is rather permissive and you may only need POST or a specific origin for example. 虽然可以根据需要进行自定义,但下面的示例相当宽松,例如,您可能只需要POST或特定来源。
Header set Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods "GET, POST, OPTIONS, DELETE"
Header add Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.