Error: Access to Xmlhttprequest at 'file://...' from origin 'null' has been blocked by CORS policy

Asked

Viewed 13,159 times

4

Hello, I am trying to add an html file inside another page through a directive. And give this error:

Access to Xmlhttprequest at 'file://.../Angularjs/Formul%C3%A1rio%20Simples/views/client.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for Protocol schemes: http, data, Chrome, Chrome-Extension, https.

Directive:

app.directive('myClient', function() { 
return { 
  templateUrl: 'views/client.html' 
}; });

Index:

<!DOCTYPE html>
<html lang="pt-br" xml:lang="pt-BR">
<head>
    <meta charset="UTF-8">
    <title>Estudos - AngularJs</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.min.js" ></script>
    <script src="js/app.js" ></script>
    <!--Controllers-->
    <script src="js/controllers/Client.js" ></script>
    <!--Services-->
    <script src="js/services/emails.js" ></script>
    <!--Directives-->
    <script src="js/directives/client.js" ></script>
</head>
<body ng-app="myApp">
    <my-client></my-client>
</body>
</html>
  • Run your page without being by the two clicks in the file. Otherwise it opens with file:///. Try running on a server.

  • Apparently it is because of that the problem even, I thought that still would not need server.

2 answers

1


  • Hello, that’s exactly why the server is missing. Now it’s working fine, thank you Alef. :)

0

I know that the answer is already accepted, but there is an addendum for those who already have the shaman installed. Start apache and move the files to htdocs.

now just access this index via localhost/index.html

so while trying to make this request will no longer try to access a file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.