【问题标题】:I am getting error as angular.js:12520TypeError: Cannot read property 'then' of undefined..我收到错误,因为 angular.js:12520TypeError: Cannot read property 'then' of undefined..
【发布时间】:2016-11-02 06:16:57
【问题描述】:
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 方法的函数。

【问题讨论】:

    标签: javascript angularjs promise


    【解决方案1】:

    你需要从logedin function()返回b.promise

    return {
        logedin: function (Uname, Password, $scope) {
           var b = $q.defer();
    
           $.ajax({
               . . . 
           });
    
           return b.promise;
        }
    }
    

    【讨论】:

      【解决方案2】:

      你错过了在 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',
                      ...
      

      【讨论】:

      • $.ajax 返回一个承诺?
      猜你喜欢
      • 2021-09-25
      • 2020-09-20
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多