2016-06-30 39 views
-2
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新手?幫我擺脫這個錯誤我得到的錯誤爲angular.js:12520TypeError:無法讀取屬性'then'undefined ..

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#webmethod的函數。

回答

0

你需要返回從logedin()函數

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

     $.ajax({ 
      . . . 
     }); 

     return b.promise; 
    } 
} 
+0

非常感謝....我得到了答案。 –

+0

@SajjaddHussain也許你可以接受它。 – Shreevardhan

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', 
       ... 
+0

$就返回一個承諾b.promise回報? –

相關問題