角度http请求

时间:2016-05-22 05:47:03

标签: asp.net angularjs asp.net-mvc asp.net-web-api

我正在使用带有角度js的asp.net Web服务,返回一个客户列表 $ http获得成功,但是当我在下面的html上迭代时,数据不显示

<div ng-app="crudModule" ng-controller="CRUD_OperController" name="myForm" novalidate class="my-form">
    <ol data-ng-repeat="cus in Customers">
        <li>{{ cus.CustomerId }} ,{{ cus.CustomerName }},{{  cus.CustomerAddress }}
    </ol>
</div>

有任何绑定问题吗?

我的脚本在

下面
<script  type="text/javascript">
    var app = angular.module('crudModule', []);

    app.service('studentService', function ($http) {
        this.getAllCustomers = function () {
        var response = $http.get("CustomerService.asmx/GetCustomers");
        return response;
        };
    });


    app.controller('CRUD_OperController', function ($scope, studentService) {
        $scope.Customers = [];

        loadData();

        function loadData() {
            var promiseGet = studentService.getAllCustomers();
            promiseGet.then(
                function (pl) {
                    $scope.Customers = pl.data;
                },
                function (errorPl) {
                    $log.error('Some Error in Getting Records.', errorPl);
                }
            );
        }

    }); 

</script>

当写console.info(pl.data);数据如下

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <Customers>
    <CustomerId>1</CustomerId>
    <CustomerName>Hamid</CustomerName>
    <CustomerAddress>Dhaka</CustomerAddress>
  </Customers>
  <Customers>
     <CustomerId>2</CustomerId>
     <CustomerName>Milon</CustomerName>
     <CustomerAddress>Dhaka</CustomerAddress>
  </Customers>
 <Customers>
      <CustomerId>3</CustomerId>
      <CustomerName>Jakir</CustomerName>
      <CustomerAddress>Dhaka</CustomerAddress>
 </Customers>
  <Customers>
     <CustomerId>4</CustomerId>
     <CustomerName>Hamid</CustomerName>
     <CustomerAddress>Khulna</CustomerAddress>
  </Customers>
  </ArrayOfCustomers>

1 个答案:

答案 0 :(得分:0)

Live Demo 为你。

我建议您angular-xml处理<xml>角度响应。

angular-xml更多细节,依赖x2js

  1. x2jsangular-xml添加到您的项目中。

  2. xml模块注入您的应用并配置httpProvider

  3.   

    HTTP拦截器会将您的所有XML响应转换为JavaScript对象。

    var app = angular.module('crudModule', ['xml']);
    
    app.config(function($httpProvider) {
      $httpProvider.interceptors.push('xmlHttpInterceptor');
    });
    
    1. 请注意您的回复数据结构。
    2.   

      通过console.info(pl),检查从xml

      解析的结果对象的嵌套结构
      function loadData() {
          var promiseGet = studentService.getAllCustomers();
          promiseGet.then(
            function(pl) {
              console.info(pl);
              $scope.Customers = pl.data.ArrayOfCustomers.Customers;
            },
            function(errorPl) {
              $log.error('Some Error in Getting Records.', errorPl);
            }
          );
        }