开发者社区> 问答> 正文

angularjs,代码完全从w3cschool里扒下来的但是不起作用

screenshot
就是这个代码,想试试$http 获取json数据来着,但就是无法显示困扰好久了浏览器的控制审查报错已阻止跨源请求:同源策略禁止读取位于 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 的远程资源。(原因:CORS 头缺少 'Access-Control-Allow-Origin')。如果设置路径为本地json的话,完全没有反应求教啊求教

展开
收起
杨冬芳 2016-07-05 16:03:08 2326 0
1 条回答
写回答
取消 提交回答
  • IT从业

    跨域了,并且 http://www.runoob.com/try/angularjs/data/Customers_JSON.php 这个地址不允许跨域请求,当然会报错。可以用雅虎的YQL,Yahoo这个地址允许跨域请求,不过注意设置Access-Control-Allow-Origin为*允许跨域,IE跨域对象是XDomaiRequest对象,XMLHttpRequest对象只有IE11才支持

     <!DOCTYPE html>
    <html>
    <body>
    
        <div ng-app="" ng-controller="customersController">
    
            <ul>
                <li ng-repeat="x in names">
                    {{ x.Name + ', ' + x.Country }}
                </li>
            </ul>
    
        </div>
    
        <script>
            function customersController($scope, $http) {
    
                $http({
                    method:'GET',
                    url: "http://query.yahooapis.com/v1/public/yql",
                    params: {
                        q: "select * from json where url=\"http://www.runoob.com/try/angularjs/data/Customers_JSON.php\"",
                        format: "json"
                    }
                }).success(function (response) {
                    console.log(response)
                    $scope.names = response.query.results.json.json;
                });
            }
        </script>
    
        <script src="http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
    
    </body>
    </html>
    2019-07-17 19:51:33
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Angular从零到一 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载