sinat_25564411 2015-07-25 16:50 采纳率: 33.3%
浏览 3188
已采纳

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

 <!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.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
  .success(function(response) {$scope.names = response;});
}
</script>

<script src="http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>

</body>
</html>

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

如果设置路径为本地json的话,完全没有反应
求教啊求教

  • 写回答

5条回答 默认 最新

  • Go 旅城通票 2015-07-26 06:05
    关注

    跨域了,并且 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记