Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Js Routing Google Chrome Issue

Worked on basic routing in angular Js with Code1 mentioned below and getting "XMLHttpRequest cannot load Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource" error and founded that we must use local web server to resolve the same and downloaded MAMP and kept my html[login.html] in htdocs folder started the server and replaced templateUrl with [localhostURL/AngularJs/login.html'] as mentioned in Code2 and getting error of Error: [$sce:insecurl] exact error are given below, Guide me with any solution to fix the same in Google Chrome...

Code 1

index.html

<!DOCTYPE html>
 <html>
    <head>
    <title>Test</title>
    </head>
    <body ng-app="plunker">
    <div ng-view=""></div>    
    <script src="angular.min.js"></script>
    <script src="angular-route.js"></script>
    <script src="app.js"></script>  
   </body>
 </html>

app.js

  var app = angular.module('plunker', ['ngRoute']);
  app.config(function ($routeProvider) {
  $routeProvider.when('/login',{
        controller: '',
        templateUrl: 'login.html'
    }).otherwise({ 
        redirectTo: '/login' 
    });
 });

login.html

   Hello from login! 

Code2

All other thing are same with changes only in app.js

   var app = angular.module('plunker', ['ngRoute']);
   app.config(function ($routeProvider) {
   $routeProvider.when('/login',{
        controller: '',
        templateUrl: 'http://localhost:8888/AngularJs/login.html'
    }).otherwise({ 
        redirectTo: '/login' 
    });
});

Error Code1:- XMLHttpRequest cannot load file://localhost/Users/karthicklove/Documents/Aptana%20Studio%203%20Workspace/Object_Javascript/Angular_Js/Routing/login.html. Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https, chrome-extension-resource.

Error Code2:- [$sce:insecurl] http://errors.angularjs.org/1.2.26/$sce/insecurl?p0=http%3A%2F%2Flocalhost%3A8888%2FAngularJs%2Flogin.html at Error (native)

like image 412
Vinoth Kumar Subramanian Avatar asked Nov 03 '14 10:11

Vinoth Kumar Subramanian


2 Answers

I got the similar error. I got a solutions for this, we cannot use Routing in AngularJS by keeping it in your file system. (i.e)file:///C:/Users/path/to/file.html?r=4383065' If you run with this URL you will get error. The route provider will use http protocol. So you have to put your code inside the localhost and then run from browser localhost/foldername/index.html . Don't run it from the folder directly

And also change your template URL like this

     templateUrl: '/AngularJs/login.html'

If you have doubt post here.

like image 109
Anbarasu Avatar answered Oct 16 '22 18:10

Anbarasu


If you are running a page directly from Chrome (you just double clicked on an html file) and your javascript is trying to request some data you will hit this error.

Reason : $routeProvider uses HTTP service for requesting data and this request cant be sent unless you are using any server like Tomcat.

Solution :

  1. Deploy your app in any server like Tomcat in your machine and open your page through server.

  2. If you think your just playing around with Client-Side coding then better open file in other browsers like Firefox / Safari.

like image 27
BeingSuman Avatar answered Oct 16 '22 18:10

BeingSuman