4-6 AngularJS1.6 跨域

AngularJS1.6 跨域

配置跨域白名单,要配置请求域名下所有内容

//配置跨域白名单
    app.config(['$sceDelegateProvider', function ($sceDelegateProvider) {
        $sceDelegateProvider.resourceUrlWhitelist([
            'self',
            // ** 代表可以访问这个域名下的所有内容
            'http://datainfo.duapp.com/**'
        ]);
    }]);

否则会发生错误:


<script src="angular.js"></script>
<!--<script src="angular1.6.js"></script>-->
<script>
    //1.创建模块
    var app = angular.module('app', []);

    //配置
    app.config(['$sceDelegateProvider', function ($sceDelegateProvider) {
        $sceDelegateProvider.resourceUrlWhitelist([
            'self',
            'http://datainfo.duapp.com/**'// ** 代表可以访问这个域名下的所有内容
        ]);
    }]);

    //2.创建控制器
    app.controller('skController', ['$scope', '$http',function ($scope, $http) {
        //1.6 以上
        $http({
            url:'http://datainfo.duapp.com/shopdata/getGoods.php',
            method:'jsonp'

        }).then(function (res) {
            console.log(res);
        }).catch(function (error) {
            console.log(error);
        });


        //1.6 以下
        /*$http({
            url:'http://datainfo.duapp.com/shopdata/getGoods.php',
            method:'jsonp',
            params:{
                callback:'JSON_CALLBACK'
            }

        }).success(function (res) {
            console.log(res);
        }).error(function (error) {
            console.log(error);
        })*/

    }]);

</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容