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:25

My problem was resolved just by adding http:// to my url address. for example I used http://localhost:3000/movies instead of localhost:3000/movies.

查看更多
明月照影归
3楼-- · 2019-01-01 05:26

I had the same issue. after adding below code to my app.js file it fixed.

var cors = require('cors')
app.use(cors());
查看更多
美炸的是我
4楼-- · 2019-01-01 05:27

The operation is not allowed in chrome. You can either use a HTTP server(Tomcat) or you use Firefox instead.

查看更多
呛了眼睛熬了心
5楼-- · 2019-01-01 05:27

If for whatever reason you cannot have the files hosted from a webserver and still need some sort of way of loading partials, you can resort to using the ngTemplate directive.

This way, you can include your markup inside script tags in your index.html file and not have to include the markup as part of the actual directive.

Add this to your index.html

<script type='text/ng-template' id='tpl-productColour'>
 <div class="list-group-item">
    <h3>Hello <em class="pull-right">Brother</em></h3>
</div>
</script>

Then, in your directive:

app.directive('productColor', function() {
      return {
          restrict: 'E', //Element Directive
          //template: 'tpl-productColour'
          templateUrl: 'tpl-productColour'
      };
   }
  );
查看更多
与君花间醉酒
6楼-- · 2019-01-01 05:32

VERY SIMPLE FIX

  1. Go to your app directory
  2. Start SimpleHTTPServer


In the terminal

$ cd yourAngularApp
~/yourAngularApp $ python -m SimpleHTTPServer

Now, go to localhost:8000 in your browser and the page will show

查看更多
流年柔荑漫光年
7楼-- · 2019-01-01 05:32

RootCause:

File protocol does not support cross origin request for Chrome

Solution 1:

use http protocol instead of file, meaning: set up a http server, such as apache, or nodejs+http-server

Sotution 2:

Add --allow-file-access-from-files after Chrome`s shortcut target, and open new browse instance using this shortcut

enter image description here

Solution 3:

use Firefox instead

查看更多
登录 后发表回答