【发布时间】:2014-12-29 23:22:38
【问题描述】:
使用下面提到的 Code1 处理 Angular Js 中的基本路由并获得“XMLHttpRequest 无法加载跨源请求仅支持协议方案:http、data、chrome-extension、https、chrome-extension-resource" 错误并发现我们必须使用本地 Web 服务器来解决相同的问题并下载 MAMP 并将我的 html[login.html] 保存在 htdocs 文件夹中启动服务器并将 templateUrl 替换为 [localhostURL/AngularJs/login.html'] 作为在 Code2 中提到并得到 Error: [$sce:insecurl] 的错误如下,请指导我使用任何解决方案在 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 中发生了变化
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 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.
错误代码2:- [$sce:insecurl] http://errors.angularjs.org/1.2.26/$sce/insecurl?p0=http%3A%2F%2Flocalhost%3A8888%2FAngularJs%2Flogin.html 出现错误(本机)
【问题讨论】:
-
在您的第二个代码中,您无需提供完整的templateUrl url,只需提供html文件名即可。
标签: angularjs cross-domain angular-ui-router