好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Angular $http解析通过接口获得的json数据

  刚接触angular不久,对很多东西都不了解,今天需要用angular通过接口得到json数据,折腾了好久,总算是能获取到数据了,下面是部分源码,仅供参考:

 HTML部分:

 1   <  body   ng-app  ="httpApp"  > 
 2           <  ul   ng-controller  ="httpController"  > 
 3               <  li   ng-repeat  ="(key,value) in infos"  > 
 4                   {{ key }} : {{ value}}
  5               </  li  > 
 6               <  span  > {{num}} </  span  > 
 7           </  ul  > 
 8   </  body  > 

javascript部分:

  1   var  app = angular.module('httpApp' ,[]);
   2      app.controller('httpController',  function   ($scope,$http) {
   3           var  nprUrl = 'http://localhost:915/API/showPage/3dde3ff80b78487184d/123' ;
   4           $http({
   5              method: 'JSONP' ,                           //使用JSONP跨域
   6              url: nprUrl + '?callback=JSON_CALLBACK'     //必须加上‘?callback=JSON_CALLBACK’,得在后台处理callback,
                                   //使返回的json格式为 JSON_CALLBACK({}),我得到的不为JSON_CALLBACK,为angular.callback._0,
                                   //具体怎么用,这块没有特别明白,欢迎有了解的告知 7 }).success( function (data, status) { 8 $scope.infos = data; 9 10 }).error( function (data, status) { 11 // Some error occurred 12 }); 13 });

  可能有错误或不恰当的地方,欢迎指正或探讨,谢谢!

 

--------------------------

   Miracle

    

查看更多关于Angular $http解析通过接口获得的json数据的详细内容...

  阅读:33次