在下面的代码中,在else if中,进行http调用,然后控件进入下一个位置(另一个控制器)。有没有办法可以让http调用完成然后转到reponse.data实现的那个部分,然后转到下一部分代码?我可以观察到,经过几个步骤后,它将进入reponse.data部分,但我希望在调用之后立即发生。
return $q(function(resolve, reject) {
//var testEnv = envService.environment == 'development' || envService.environment == 'ci';
testEnv = false;
if(testEnv) {
var user = _.find(self.getHardCodedUsers(), function(u) { return u.id == id; });
if(user) {
resolve(user);
} else {
reject('No user was found');
}
}
else if($rootScope.plannerRedirect){
var url = SessionService.jsonUrl();
$http({
url: url + '/user/plannerclient/profile/' + id,
method: "GET",
headers: {
'Authorization': 'Bearer ' + self.getToken()
}
}).then(
function(response) {
if(response.data) {
resolve(response.data);
} else {
reject('No user was found');
}
}, function errorCallback(response) {
reject('Sorry, the query could not be performed. Please check your network connection.');
});
}
else {
var url = SessionService.jsonUrl();
$http({
url: url + '/user/profile/' + id,
method: "GET",
headers: {
'Authorization': 'Bearer ' + self.getToken()
}
}).then(
function(response) {
if(response.data) {
resolve(response.data);
} else {
reject('No user was found');
}
}, function errorCallback(response) {
reject('Sorry, the query could not be performed. Please check your network connection.');
});
}
});
答案 0 :(得分:0)
尝试使用.success(function(){});
代替.then(function(){});
您需要来自服务器的回调,状态代码为2xx 200(ok),201(已创建),202(已接受)。