5

我是新手AngularJs并在网络表单中使用它

我的代码如下

var app = angular.module('demoApp', [])
app.controller('usrController', function ($scope, $http, $window) {
    $scope.userdata = {};
    var post = $http({
        method: "POST",
        url: "Index.aspx/GetData",
        dataType: 'json',
        data: {},
        headers: { "Content-Type": "application/json" }
    }).success(function (data, status) {
        console.log(data);
        $scope.userdata = data.d;
    }).error(function (data, status) {
        $window.alert(data.Message);
    });
});

我的 WebMethod 代码如下

[WebMethod]
public static string GetData()
{
    DataTable dt = Helper.UserList("sp_GetSearchList");
    string JSONString = string.Empty;
    JSONString = JsonConvert.SerializeObject(dt).ToString();
    return JSONString;
}

我的 JSONString 返回完美的 json 但我收到错误

TypeError: $http(...).success 不是函数

我在 Stack Overflow 上看到了很多答案,但没有一个真正解决了这个问题。

扩展问题

使用以下代码后,它解决了我的问题

var app = angular.module('demoApp', [])
app.controller('usrController', function ($scope, $http, $window) {
    $scope.userdata = {};
    $http.post("Index.aspx/GetData", {}).
    then(function (response) {
        console.log(JSON.parse(response.data.d));
        $scope.userdata = JSON.parse(response.data.d);
    });
}, 
function(error) {

});

我的前端绑定是这个

<body data-ng-app="demoApp">
    <form id="form1" runat="server">
        <div data-ng-controller="usrController">
             <table>
                 <tr>
                     <th>Sl No</th>
                     <th>User ID</th>
                     <th>Employee ID</th>
                     <th>Employee Name</th>
                     <th>Birth Date</th>
                 </tr>
                 <tr data-ng-repeat="data in userdata">
                    <td>{{data.ID}}</td>
                    <td>{{data.UserID}}</td>
                    <td>{{data.EmployeeID}}</td>
                    <td>{{data.EmpName}}</td>
                    <td>{{data.BirthDate}}</td>
                 </tr>
             </table>
        </div>
    </form>
</body>

数据完美输入,console.log但在前端没有绑定。

帮助我哪里错了。在此先感谢

4

3 回答 3

5

Angular 1.6中,$http服务发生了变化:你不能再使用.successand.error了。

相反,使用.then. 来自$http文档

$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });
于 2017-06-21T07:16:20.890 回答
3

应该是这样的

 $http({
    method: "POST",
    url: "Index.aspx/GetData",
    dataType: 'json',
    data: {},
    headers: { "Content-Type": "application/json" }
}).then(function(result) {
  //Success
 }, function(error) {
 //Error
 });  
于 2017-06-21T07:18:06.683 回答
1

下面试试。

 $http.post( "Index.aspx/GetData", {} )
   .then(function(response) {
      $scope.userdata = response.data;
   });
于 2017-06-21T07:15:40.713 回答