简体   繁体   English

所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问源“ file://”。

[英]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.

相关问题 Javascript:“所请求的资源上没有'Access-Control-Allow-Origin'标头。 因此,不允许访问原始“空”。 ” - Javascript: “ No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. ” 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access AngularJS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - AngularJS : No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access Golang 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许访问原点 'null' - Golang No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin '...' 因此不允许访问 - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '…' is therefore not allowed access XMLHttpRequest无法加载。 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许原点访问 - XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin is therefore not allowed access Vanilla JS:所请求的资源上不存在“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“空” - Vanilla JS: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost:4400” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4400' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此不允许访问源 'http://127.0.0.1:8080' - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM