Javascript AngularJS-烤箱在控制器中不工作
Service.jsJavascript AngularJS-烤箱在控制器中不工作,javascript,angularjs,angular-promise,angularjs-http,Javascript,Angularjs,Angular Promise,Angularjs Http,Service.js this.userLogin = function (username, password) { var dataBody = $.param({'username': username,'password': password}); return $http({ method: 'POST', url: servicePathURL, data: dataBody,
this.userLogin = function (username, password) {
var dataBody = $.param({'username': username,'password': password});
return $http({
method: 'POST',
url: servicePathURL,
data: dataBody,
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
}).catch(function (error) {
throw error;
});
};
AuthenticationServiceLogin.userLogin($scope.username, $scope.password)
.then(function (response) {
if (response.status ==200) {
toaster.pop('success', "", "Login Successful");
$location.path('/home');
}
}).catch(function (error) {
toaster.pop('error', "", error.statusText);
});
this.userLogin = function (username, password) {
return $http({
method: 'POST',
url: servicePathURL,
data: $.param({'username': username,'password': password}),
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
// The catch block here is useless. The promise returned by $http will transmit any
// errors on its own.
//}).catch(function (error) {
// throw error;
});
};
Controller.js
this.userLogin = function (username, password) {
var dataBody = $.param({'username': username,'password': password});
return $http({
method: 'POST',
url: servicePathURL,
data: dataBody,
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
}).catch(function (error) {
throw error;
});
};
AuthenticationServiceLogin.userLogin($scope.username, $scope.password)
.then(function (response) {
if (response.status ==200) {
toaster.pop('success', "", "Login Successful");
$location.path('/home');
}
}).catch(function (error) {
toaster.pop('error', "", error.statusText);
});
this.userLogin = function (username, password) {
return $http({
method: 'POST',
url: servicePathURL,
data: $.param({'username': username,'password': password}),
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
// The catch block here is useless. The promise returned by $http will transmit any
// errors on its own.
//}).catch(function (error) {
// throw error;
});
};
toaster.pop('error','',error.statusText)当用户登录时出现异常时,不会调用代码>
$http
方法,返回有什么好处吗
$q.defer()
承诺,而不是$http
承诺或被视为最佳实践?如果是,我如何将上面的$http
代码修改为promise
您的代码似乎很好。只要重新抛出
$http
调用遇到的任何错误,它们就会一直传播到控制器。我倾向于说,错误处理的任何问题都不在您发布的代码中
如果您不打算处理抛出的错误,那么在service.js中使用catch处理程序没有任何好处。Service.js将希望如下所示:
Service.js
this.userLogin = function (username, password) {
var dataBody = $.param({'username': username,'password': password});
return $http({
method: 'POST',
url: servicePathURL,
data: dataBody,
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
}).catch(function (error) {
throw error;
});
};
AuthenticationServiceLogin.userLogin($scope.username, $scope.password)
.then(function (response) {
if (response.status ==200) {
toaster.pop('success', "", "Login Successful");
$location.path('/home');
}
}).catch(function (error) {
toaster.pop('error', "", error.statusText);
});
this.userLogin = function (username, password) {
return $http({
method: 'POST',
url: servicePathURL,
data: $.param({'username': username,'password': password}),
headers: {
"Authorization": "Basic",
"Content-Type": "application/x-www-form-urlencoded"
}
})
.then(function (response) {
$rootScope.globals = {
currentUser: {
username: username,
}
};
return response;
// The catch block here is useless. The promise returned by $http will transmit any
// errors on its own.
//}).catch(function (error) {
// throw error;
});
};
回答您的第二个问题:使用$q.defer()
而不是返回$http
本身返回的承诺没有任何好处,还有一些主要缺点,即登录方法引发的任何异常都将消失。有关详细信息,请参见此处的延迟反模式:
$http
无疑是首选选项。您遇到的异常是从哪里抛出的?假设没有任何其他catch调用隐藏在某个地方使用该错误,则错误应该可以正常传播。在controllers catch函数中没有调用toaster。您是否尝试删除service.js中的catch表达式?如果我从service.js中删除catch,我看不到在controller中调用catch块。那么在service.js中调用.catch(…)吗?看起来不是这样的吗?考虑到上面的代码片段,你们能发布相关的代码吗?您建议坚持使用$http
是优于%q
的首选选项?如果我从service.js中删除catch块,错误不会传播到controllers catch块。我如上所述更新了代码并从service.js中删除了catch。虽然错误传播到控制器的catch块,但toaster在那里仍然不工作toaster.pop('error','',error.statusText)代码>我还能利用服务捕获块中抛出的错误吗?对不起,你是说toaster.pop(…)
不起作用,还是说执行根本不起作用?关于catch块,我的观点是,在Service.js中完全没有必要有一个catch处理程序——如果您真的要处理错误,比如记录错误,或者向用户显示错误,那么只应该包含catch处理程序。如果包含从服务器发送的HTTP响应的详细信息,这将非常有用。是401/200/404吗?是的,执行将进入控制器捕获块,我也可以从服务器记录错误。当凭据无效时,HTTP响应为401,而正确的凭据将用户重定向到带有成功登录toaster消息的主页。但是对于服务器错误,当它转到控制器时,toaster没有被生成。catch blocktoaster只是一个被注入控制器的服务,对吗?关于controller.js中身份验证调用的代码的任何额外信息都会很有帮助。