我收到错误为angular.js:12520TypeError:无法读取属性'然后'未定义..

时间:2016-06-30 08:51:29

标签: javascript angularjs promise

angular.js:12520TypeError: Cannot read property 'then' of undefined
at r.$scope.fetch (authentication.js:551)
at fn (eval at compile (angular.js:13365), <anonymous>:4:206)
at e (angular.js:23613)
at r.$eval (angular.js:16052)
at r.$apply (angular.js:16152)
at HTMLInputElement.<anonymous> (angular.js:23618)
at HTMLInputElement.dispatch (jquery.js:4670)
at HTMLInputElement.r.handle (jquery.js:4338)(anonymous function) @ angular.js:12520

如果我是Promise的新手,如何解决这种情况?帮我摆脱这个错误

helper.logedin(Uname, Password).then(function (result) {
            helper.showLoading();
        }).catch(function (failure) { alert(failure); })
.finally(function () {
    this.$scope.$apply();
});

这是在单击按钮时我正在调用的函数的代码。

angular.module('authentication', ['messageHandling']) 
.factory('helper', function ($http, $q, handler) { 
    return { logedin: function (Uname, Password, $scope) {
            var b = $q.defer();

            $.ajax({
                type: 'POST',
                url: siteUrl + '/login.aspx/logedin',
                data: JSON.stringify({ uname: Uname, password: Password }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                success: function (data, status) {
                    var sessionId = $.parseJSON(data.d).customerId;
                    if (sessionId != "") {
                        var customerName = $.parseJSON(data.d).customerName;
                        var goToUrl = $.urlParam("goToUrl");
                        var PrevPage = $.urlParam("PrevPage");

                        $.cookie("userSession", sessionId, { path: '/', expires: 1 });
                        $.cookie("userName", customerName, { path: '/', expires: 1 });

                        switch (goToUrl) {
                            case undefined:
                                window.location.href = window.location.origin + "/" + "category.aspx";
                                break;
                            case !undefined:
                                window.location.href = window.location.origin + "/" + goToUrl + PrevPage;
                                break;
                            default:
                                window.location.href = decodeURIComponent(goToUrl) + "?&PrevPage=" + PrevPage;

                                break;
                        }
                    }
                    else {
                        handler.showMessage('E', 'Login Fail!', 'User Id or Passwrod is incorrect.', $scope);
                        b.reject("Failure");
                    }

                },
                failure: function (data, status) {
                    handler.showMessage('E', 'Fail!', 'Something Went wrong, Try Again', $scope);
                    b.reject("Failure");
                },
                error: function (data, status) {
                    handler.showMessage('E', 'Fail!', 'Something Went wrong, Try Again', $scope);
                    b.reject("Failure");
                },

            });
        },

这是调用c#的web方法的函数。

2 个答案:

答案 0 :(得分:0)

你错过了在ajax请求上添加回复

angular.module('authentication', ['messageHandling']) 
.factory('helper', function ($http, $q, handler) { 
    return { logedin: function (Uname, Password, $scope) {
            var b = $q.defer();

            return $.ajax({
                type: 'POST',
                ...

答案 1 :(得分:0)

您需要从b.promise function()

返回logedin
return {
    logedin: function (Uname, Password, $scope) {
       var b = $q.defer();

       $.ajax({
           . . . 
       });

       return b.promise;
    }
}