angularjs $http.get()

remyyoung 发布于 2015/10/16 22:08
阅读 2K+
收藏 0

todo.json文件,内容如下:

[
{
"Name" : "Alfreds Futterkiste",
"City" : "Berlin",
 "Country" : "Germany"
},
{
"Name" : "Berglunds snabbköp",
"City" : "Luleå",
"Country" : "Sweden"
},
{
"Name" : "Centro comercial Moctezuma",
"City" : "México D.F.",
"Country" : "Mexico"
},
{
 "Name" : "Ernst Handel",
"City" : "Graz",
"Country" : "Austria"
},
 {
"Name" : "FISSA Fabrica Inter. Salchichas S.A.",
"City" : "Madrid",
 "Country" : "Spain"
},
{
"Name" : "Galería del gastrónomo",
 "City" : "Barcelona",
"Country" : "Spain"
},
{
"Name" : "Island Trading",
"City" : "Cowes",
"Country" : "UK"
},
{
"Name" : "Königlich Essen",
"City" : "Brandenburg",
"Country" : "Germany"
},
{
 "Name" : "Laughing Bacchus Wine Cellars",
"City" : "Vancouver",
 "Country" : "Canada"
},
{
"Name" : "Magazzini Alimentari Riuniti",
 "City" : "Bergamo",
"Country" : "Italy"
},
{
"Name" : "North/South",
"City" : "London",
"Country" : "UK"
},
{
 "Name" : "Paris spécialités",
"City" : "Paris",
"Country" : "France"
 },
{
"Name" : "Rattlesnake Canyon Grocery",
"City" : "Albuquerque",
 "Country" : "USA"
},
{
"Name" : "Simons bistro",
"City" : "København",
 "Country" : "Denmark"
},
{
"Name" : "The Big Cheese",
"City" : "Portland",
"Country" : "USA"
},
{
"Name" : "Vaffeljernet",
 "City" : "Århus",
"Country" : "Denmark"
},
{
"Name" : "Wolski Zajazd",
"City" : "Warszawa",
"Country" : "Poland"
}
]

demojs.html文件,内容如下

<!DOCTYPE html>
<html ng-app="demo">
 <head>
  <title>Example</title>

    //引入angular.js,bootstrap.css,bootstrap-theme.css文件
  <script src="angular.js"></script>

  <link href="bootstrap.css" rel="stylesheet" />
  <link href="bootstrap-theme.css" rel="stylesheet" />
  <script type="text/javascript">
   var myApp = angular.module("demo",[]);


   myApp.controller("demoCtrl",function($scope,$http){
    
    var promise = $http.get("todo.json");


    promise.success(function(data){
     $scope.todos = data;
     console.log("加载成功");
    }).error(function(){
     console.log("加载失败");
    });
   });
 
  </script>
 </head>
 <body ng-controller="demoCtrl">
  <div class="panel">
   <h1>to do</h1>
   <table class="table">
    <thead>
     <tr>
      <th>Name</th>
      <th>City</th>
      <th>Country</th>
     </tr>
    </thead>
    <tr ng-repeat="item in todos">
     <td>{{item.Name}}</td>
     <td>{{item.City}}</td>
     <td>{{item.Country}}</td>
    </tr>
   </table>
  </div>
 </body>
</html>

问题:加载todo.json文件失败,数据不能显示!

发现问题联系我964929101@qq.com,谢谢了!

加载中
0
lieefu
lieefu
get方法默认不能获取数组,需要设置
0
remyyoung
remyyoung
具体要怎么设置,我看了一些资料,没有找到?
0
remyyoung
remyyoung
todo.json没有加载成功,没有执行success方法,直接执行的error方法,后台打印“加载失败”
0
純白陰影
純白陰影

浏览器问题,不能访问本地文件,弄个服务器跑起来就可以了

或者给Chrome加启动参数,参考:http://my.oschina.net/gaollg/blog/81225

0
remyyoung
remyyoung

引用来自“純白陰影”的评论

浏览器问题,不能访问本地文件,弄个服务器跑起来就可以了

或者给Chrome加启动参数,参考:http://my.oschina.net/gaollg/blog/81225

谢谢!


添加启动参数后,打开添加启动参数的chrome,在地址栏输入绝对路径C:/Users/Administrator/Desktop/angularjs/jsdemo.html,就可以了!

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部