AngularJS Error: Cross origin requests are only su

2019-01-01 04:52发布

I have three files of a very simple angular js application

index.html

<!DOCTYPE html>
<html ng-app="gemStore">
  <head>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js'></script>
    <script type="text/javascript" src="app.js"></script>
  </head>

  <body ng-controller="StoreController as store">
      <div class="list-group-item" ng-repeat="product in store.products">
        <h3>{{product.name}} <em class="pull-right">{{product.price | currency}}</em></h3>
      </div>

  <product-color></product-color>
  </body>
</html>

product-color.html

<div class="list-group-item">
    <h3>Hello <em class="pull-right">Brother</em></h3>
</div>

app.js

(function() {
  var app = angular.module('gemStore', []);

  app.controller('StoreController', function($http){
              this.products = gem;
          }
  );

  app.directive('productColor', function() {
      return {
          restrict: 'E', //Element Directive
          templateUrl: 'product-color.html'
      };
   }
  );

  var gem = [
              {
                  name: "Shirt",
                  price: 23.11,
                  color: "Blue"
              },
              {
                  name: "Jeans",
                  price: 5.09,
                  color: "Red"
              }
  ];

})();

I started getting this error as soon as I entered an include of product-color.html using custom directive named productColor:

XMLHttpRequest cannot load file:///C:/product-color.html. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.
angular.js:11594 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/product-color.html'.

What may be going wrong? Is it a path issue for product-color.html?

All my three files are in the same root folder C:/user/project/

16条回答
零度萤火
2楼-- · 2019-01-01 05:33
  1. Install the http-server, running command npm install http-server -g
  2. Open the terminal in the path where is located the index.html
  3. Run command http-server . -o
  4. Enjoy it!
查看更多
浅入江南
3楼-- · 2019-01-01 05:36

there is a chrome extension 200ok its a web server for chrome just add that and select your folder

查看更多
临风纵饮
4楼-- · 2019-01-01 05:37

This issue is not happening in Firefox and Safari. Make sure you are using the latest version of xml2json.js. Because i faced the XML parser error in IE. In Chrome best way you should open it in server like Apache or XAMPP.

查看更多
千与千寻千般痛.
5楼-- · 2019-01-01 05:37

You have to open chrome using the following flag Go to run menu and type "chrome --disable-web-security --user-data-dir"

Make sure all the instances of chrome are closed before you use the flag to open chrome. You will get a security warning that indicates CORS is enabled.

查看更多
爱死公子算了
6楼-- · 2019-01-01 05:41

This error is happening because you are just opening html documents directly from the browser. To fix this you will need to serve your code from a webserver and access it on localhost. If you have Apache setup, use it to serve your files. Some IDE's have built in web servers, like JetBrains IDE's, Eclipse...

If you have Node.Js setup then you can use http-server. Just run npm install http-server -g and you will be able to use it in terminal like http-server C:\location\to\app.

查看更多
有味是清欢
7楼-- · 2019-01-01 05:45

If you are running server already, don't forget to use http:// in the API call. This might cause a serious trouble.

查看更多
登录 后发表回答