【发布时间】: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