【发布时间】:2015-08-22 16:01:41
【问题描述】:
出于某种原因,控制台总是说“不”。我已尝试将所有返回值更改为 true,但它仍然会说“不”
function checkLogin($http) {
$http.get('http://localhost/api/v1/ping').
then(function(response) {
if(response.status === 200) {
return true;
} else {
return false;
}
}, function() {
return false;
});
}
/**
* @ngInject
*/
function loginController($scope, $location, $cookies, $http) {
if(checkLogin($http) === true) {
console.log('yes');
} else {
console.log('no');
if($location.path() !== ''){
}
}
}
【问题讨论】:
-
事件序列:1 - 调用 checkLogin。 2 - 发送 Ajax 请求。 3 - checkLogin 返回。 4 - 收到 Ajax 响应。注意:函数返回(3)之前接收到Ajax响应(4)。
标签: javascript ajax json angularjs