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条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择