跨源请求仅支持 HTTP [英] Cross origin requests are only supported for HTTP

查看:27
本文介绍了跨源请求仅支持 HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行此代码:

<html ng-app="myApp"><头><title>示例 06</title><身体><!-- Diretiva ng-repeat --><div ng-controller="MyController"><a ng-href="#/">第 1 页</a>|<a ng-href="#/p2">第 2 页</a><div ng-view="#/p2"></div>

<script src="js/angular.js"></script><script src="js/angular-route.js"></script><script type="text/javascript">var myApp = angular.module('myApp', ['ngRoute']);myApp.config(function($routeProvider){$routeProvider.when('/' , {controller: 'MyController', templateUrl: 'p1.html'}).when('/p2', {controller: 'MyController', templateUrl: 'p2.html'}).otherwise({ redirectTo: '/'});});myApp.controller('MyController', function($scope){$scope.nomes = [{id:1,名称:'亚瑟'},{id: 2, 名称: 'Portos'},{id:3,名称:'阿拉米斯'}];});

但出现以下错误:

XMLHttpRequest 无法加载 file:///home/93579551515/Desktop/Angular/p1.html.跨源请求仅支持 HTTP.

我不想在网络服务器上运行它.

解决方案

您可以在 index 中添加诸如 p1.htmlp2.html 之类的模板.html 将它们放在 script 标签中,如 提到的在文档中.

然后 angular 将不再需要通过 AJAX 请求来获取它们.

请注意,这些 script 元素必须出现在 之后 ng-app.

I'm trying run this code:

<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title>Exemple 06</title>
</head>
<body>
    <!-- Diretiva ng-repeat -->
    <div ng-controller="MyController">
        <a ng-href="#/">Page 1</a> | <a ng-href="#/p2">Page 2</a>
        <div ng-view="#/p2"></div>
    </div>

    <script src="js/angular.js"></script>
    <script src="js/angular-route.js"></script>
    <script type="text/javascript">
        var myApp = angular.module('myApp', ['ngRoute']);

        myApp.config(function($routeProvider){
            $routeProvider
                .when('/'  , {controller: 'MyController', templateUrl: 'p1.html'})
                .when('/p2', {controller: 'MyController', templateUrl: 'p2.html'})
                .otherwise({ redirectTo: '/'});
        });

        myApp.controller('MyController', function($scope){
            $scope.nomes = [
                {id: 1, nome: 'Arthur'},
                {id: 2, nome: 'Portos'},
                {id: 3, nome: 'Aramis'}
            ];
        });
    </script>
</body>

but the error below occur:

XMLHttpRequest cannot load file:///home/93579551515/Desktop/Angular/p1.html. Cross origin requests are only supported for HTTP.

I don't want to run it on a webserver.

解决方案

You can add templates such as p1.html and p2.html in your index.html by putting them inside script tags, as mentioned in the docs.

<script type="text/ng-template" id="p1.html">
  This is the content of the template
</script>

Then angular will no longer need to make AJAX requests to fetch them.

Do note that these script elements must appear after ng-app.

这篇关于跨源请求仅支持 HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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