AngularJS : TypeError: $http(. ..).success 不是 asp.net WebMethod 上的函数

标签 angularjs http angularjs-1.6

我是 AngularJs 的新手,在 Web 表单中使用它

我的代码如下

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 is not a function

我看过很多关于 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) {

});

我的前端绑定(bind)是这样的

<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 中,但它在前端没有绑定(bind)。

在我错的地方帮助我。提前致谢

最佳答案

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

相反,使用.then。来自 $http docs :

$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.
  });

关于AngularJS : TypeError: $http(. ..).success 不是 asp.net WebMethod 上的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44669097/

相关文章:

javascript - Angularjs ng-disabled 无法正常工作

javascript - ng-model 外部 Controller

java - Angular 和文件上传

rest - JMeter : How to handle asynchronous http post request

html - 如何判断网页是否被修改?

javascript - Angular-route1.6.1 无法正常工作

angularjs - 如果至少选中一个复选框,则应启用 Angular JS + Button 怎么做?

javascript - ng-selected 不更新下拉列表 angularjs

http - netsh http 添加 urlacl : add reservation for a group

javascript - Angular 1.6.4 如何检测组件的无绑定(bind)属性的变化