0

Got the following problem with angularjs XMLHttpRequest cannot load file:... . Cross origin requests are only supported for HTTP My HTML:

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <script src="angular.js"></script>
    <script src="angularr.js"></script>
    <script src="app.js"></script>
</head>
    <body>
        <div ng-view></div>
    </body>
</html>

And my app.js

'use strict';
var app = angular.module("app", ['ngRoute'])
    .config(function  ($routeProvider) {
        $routeProvider.
            when("/", {templateUrl:"first.html"})
        });

What should I do?

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100
Xtal
  • 295
  • 5
  • 20
  • 1
    If you're not serving it from a webserver you should try doing that. Run `python -m SimpleHTTPServer 8080` or something like that. – mrmcgreg Mar 04 '14 at 18:31
  • No ,the files are locally – Xtal Mar 04 '14 at 18:32
  • 1
    Probably the same thing: http://stackoverflow.com/questions/18669586/angularjs-http-load-data-from-local-disk-issue-in-ie9 – mrmcgreg Mar 04 '14 at 18:35
  • Yep that is is running perfectly on firefox – Xtal Mar 04 '14 at 18:37
  • 1
    testuser had it right. If you're developing locally using just a browser to view the files, Chrome will complain. You'll either need to run a server locally or use FF. Alternately, you can use something like JSFiddle or Plunker. – Sharondio Mar 04 '14 at 19:04

1 Answers1

0

You can run a Chrome/Chromium instance with the appropriate flag, e.g.:

chromium --disable-web-security

To allow access from local files to local files, there is another remedy:

chrome --allow-file-access-from-files

See Run Chromium With Flags on the Chromium How-Tos, and here is a complete reference for the command line switches.

Disclaimer: While it's comfortable during development, this is not a substitute for running your app on an actual server, the easiest way would be launching a python HTTP server, as @testuser kindly suggested (python -m SimpleHTTPServer, or with python 3: python3 -m http.server).

Eliran Malka
  • 15,821
  • 6
  • 77
  • 100