Angular Js 路由 Google Chrome 问题 [英] Angular Js Routing Google Chrome Issue

查看:53
本文介绍了Angular Js 路由 Google Chrome 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面提到的 Code1 处理 Angular Js 中的基本路由,并得到XMLHttpRequest 无法加载跨源请求仅支持协议方案:http、数据、chrome-extension、https、chrome-extension-resource" 错误并发现我们必须使用本地 Web 服务器来解析相同的和下载的 MAMP 并将我的 html[login.html] 保存在 htdocs 文件夹中启动服务器并将 templateUrl 替换为 [localhostURL/AngularJs/login.html'] 作为在 Code2 中提到并得到 Error: [$sce:insecurl] 的错误,下面给出了确切的错误,请指导我使用任何解决方案来修复 Google Chrome 中的相同问题...

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...

代码 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! 

代码 2

所有其他的都一样,只在 app.js 中发生了变化

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' 
    });
});

错误代码 1:-XMLHttpRequest 无法加载 file://localhost/Users/karthicklove/Documents/Aptana%20Studio%203%20Workspace/Object_Javascript/Angular_Js/Routing/login.html.跨域请求仅支持以下协议方案:http、data、chrome-extension、https、chrome-extension-resource.

错误代码2:- [$sce:insecurl] http://errors.angularjs.org/1.2.26/$sce/insecurl?p0=http%3A%2F%2Flocalhost%3A8888%2FAngularJs%2Flogin.html 错误(原生)

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)

推荐答案

我遇到了类似的错误.我对此有一个解决方案,我们不能通过将路由保留在您的文件系统中来使用 AngularJS 中的路由.(i.e)file:///C:/Users/path/to/file.html?r=4383065' 如果你用这个 URL 运行你会得到错误.路由提供者将使用 http 协议.所以你必须把你的代码放在 localhost 中,然后从浏览器 localhost/foldername/index.html 运行.不要直接从文件夹运行它

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.

这篇关于Angular Js 路由 Google Chrome 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆