-------跨域

2017-06-01  本文已影响37人  MGd

跨域

http://localhost/day2/06-%E8%B7%A8%E5%9F%9F.html
https://api.douban.com/v2/book/1220562

angular跨域

    <script src="angular.js"></script>
    <script>
        //1.创建模板
        var app = angular.module('app', []);
        //2.创建控制器
        app.controller('xmgController', ['$scope','$http', function ($scope,$http) {
            $http({
                url:'https://api.douban.com/v2/book/1220562',     //访问的url地址
                method:'jsonp',                                   //访问方式
                params:{
                    callback:'JSON_CALLBACK'                     //传递过去的参数
                }
            }).success(function (res) {           
               alert(res);
            }).error(function (error) {
            });
        }]);
        //3.绑定模块
        //4.绑定控制器
    </script>
</head>
<body ng-app="app" ng-controller="xmgController">
</body>

angular1.6版本之后跨域

<script src="angular1.6.js"></script>
    <script>
        //1.创建模板
        var app = angular.module('app', []);
        //设置一个白名单。
        app.config(['$sceDelegateProvider',function ($sceDelegateProvider) {
            $sceDelegateProvider.resourceUrlWhitelist([
                'self',
                'https://api.douban.com/**']);//!*当前文件代表所有文件 **所有文件夹下的所有文件。
        }]);
        //2.创建控制器
        app.controller('xmgController', ['$scope','$http', function ($scope,$http) {
            $http({
                url:'https://api.douban.com/v2/book/1220562',
                method:'jsonp',
            }).then(function (res) {

            }).catch(function () {

            });
        }]);
        //3.绑定模块
        //4.绑定控制器
    </script>
</head>

<body ng-app="app" ng-controller="xmgController">
</body>

使用php做桥接获取数据

$url = "https://api.douban.com/v2/book/1220562";    //获取数据到服务器
echo file_get_contents($url);          //显示出来

    <script src="angular.js"></script>
    <script>
      //1.创建模板
      var app = angular.module('app', []);
      //2.创建控制器
      app.controller('xmgController', ['$scope','$http', function ($scope,$http) {
            $http({
                url:'qwl.php',                  //向谁访问数据
                method:'get'                    //用什么方式访问
            }).success(function (res) {           
                console.log(res);
            }).error(function (error) {
            });
      }]);
      //3.绑定模块
      //4.绑定控制器
    </script>
</head>
<body ng-app="app" ng-controller="xmgController">
</body>
上一篇 下一篇

猜你喜欢

热点阅读