北城已荒凉 2016-05-27 10:50 采纳率: 0%
浏览 37

Ajax函数未调用

I am trying to call web service using ajax call in AngularJS Application and want to display data. But its not reaching to function. what is the problem.

AJAX Call-

function EmpCtrl($scope) {
   $scope.getEmployee = function () {
      $.ajax({
          type: "POST",
          url: "EmpWebService.asmx/GetEmployee",
          data: "{}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (msg) {
              $(msg).find('Table').each(function (i, row) {

                  $scope.Name = $(row).find('empName').text();
                  $scope.Age = $(row).find('empAge').text();
                  $scope.City = $(row).find('empCity').text();

              });
          }
      });
  };
} 

Here is my web service

[WebMethod]
    public static string  GetEmployee()
    {
        string connectionStr =       ConfigurationManager.ConnectionStrings["connectionStr"].ConnectionString;
        SqlConnection con = new SqlConnection(connectionStr);
        SqlCommand cmd;
       DataTable dt;

        try
        {
            con.Open();
            cmd = con.CreateCommand();
            cmd.CommandText = "select * from Emp_TempTbl";
           using(SqlDataAdapter sda=new SqlDataAdapter (cmd))
           {
               using (DataSet ds=new DataSet ())
               {
                   sda.Fill(ds);
                   return ds.GetXml();
               }
           } 
        }
        catch(Exception) 
        {
            throw;
        }

And here is Angular JS UI

<html  xmlns="http://www.w3.org/1999/xhtml" ng-app="myApp">
<head runat="server">
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/jquery-1.8.3.min.js"></script>
    <script src="script.js"></script>
    <script>
        angular.module('myApp', [])
  .controller('EmpCtrl', EmpCtrl);
    </script>
</head>
<body>
    <form id="form1" ng-controller="EmpCtrl" ng-submit="save()">
        <div style="font-family: Verdana; font-size: 12px; margin-left: 450px;">
                  <div>
                <input type="button" id="btnFetch" value="Fetch" ng-click="getEmployee()" />
            </div>

        </div>
        <hr />

        <table border="1" style="text-align: center; margin-left: 410px;">
            <tr>

                <td> Name
                </td>
                <td> Age
                </td>              
                <td>City
                </td>
            </tr>
            <tr>
                <td>{{Name}}
                </td>
                <td>{{Age}}
                </td>      
                <td>{{City}}
                </td>
           </tr>
       </table>
       </form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33733810 2016-05-27 11:04
    关注

    Change controller to :

    function EmpCtrl($scope, $http) {
            var post_data = {};
            var responsePromise = $http.post("EmpWebService.asmx/GetEmployee", post_data);
            responsePromise.success(function(msg, status1, headers, config) {
                $(msg).find('Table').each(function (i, row) {
                    $scope.Name = $(row).find('empName').text();
                    $scope.Age = $(row).find('empAge').text();
                    $scope.City = $(row).find('empCity').text();
    
                  });
            });
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?