2017-02-23 141 views
3

Please find attached what I want to achieved如何管理AngularJS http请求调用

我有一段代码,发送一个HTTP双request.I希望首先进行身份验证请求,如果真的执行下一条语句(即刚刚返回$ http promise)。我怎样才能做到这一点angularJS。截至目前它正在返回undefined。

dmdb._login_request = function(credentials, conf) { 
    var p = { 
     '_method': 'POST', 
     'data[User][username]': credentials.login, 
     'data[User][password]': credentials.password 
    }; 
    conf.headers = { 
     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
    }; 
    var userData = { 
     'username': 'mbah', 
     'password': '[email protected]', 
     'applicationid': '1' 
    }; 
    $http.post('myapp.com/authenticate/', userData, con).success(function(data) { 
     if (data.success) { 
      return $http.post(this.url + 'users/login', $.param(p), conf); 
     } 
     return $q.when('failed'); 
    }) 
}; 
+2

请复制并粘贴您的代码,而不是提供的图像 – Houseman

+0

@Houseman刚刚做到了。请任何建议或提示,将不胜感激。 – mass

回答

1

我真的不明白你的问题是什么,但是, 因为它似乎你正试图从一个承诺函数返回值。

我会建议你写下面的代码:

var userData={ 
     'username':'mbah', 
     'password':'[email protected]', 
     'applicationid':'1' 
    }; 

    var deferred = $q.defer(); 

    $http.post('myapp.com/authenticate/',userData,con).success(function(data){ 
      if(data.success){ 
        $http.post(this.url+'users/login',$.param(p),conf).success(function(data2){ 
         deferred.resolve(data2); 
        }) 
      } 
      // I dont understand what this is for 
      //turn $q.when('failed'); 
    }) 

    return deferred.promise; 
}; 

这样你的函数将返回,你可以使用 你的目的的承诺。

或请详细说明更多,如果它不能帮助你

+0

感谢您的帮助。但是,我想返回数据的$ http.post(this.url +'users/login',$。param(p),conf)intead。 我有另一种通用的方法来做到这一点 – mass

3

使用承诺链来调用请求。

function firstReq(userData, conf) { 
    return $http({ 
     method: 'POST', 
     url: 'myapp.com/authenticate/', 
     headers: conf.headers, 
     data: userData 
    }) 
} 

function secondReq(p, conf) { 
    return $http({ 
     method: 'POST', 
     url: this.url + 'users/login', 
     headers: conf.headers, 
     data: $.param(p) 
    }) 
} 
$scope.processform = function() { 
    var userData = { 
     'username': 'mbah', 
     'password': '[email protected]', 
     'applicationid': '1' 
    }; 
    var p = { 
     '_method': 'POST', 
     'data[User][username]': credentials.login, 
     'data[User][password]': credentials.password 
    }; 
    conf.headers = { 
     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
    }; 
    firstReq(userData, conf) 
     .then(function(response) { 
      console.log(response.data); 
      return secondReq(p, conf); 
     }) 
     .then(function(response) { 
      console.log(response.data); 
     }) 
} 
1
my_obj._login_request = function (credentials,conf) { 


    var url_=this.url; 

     var p = { 
     '_method': 'POST', 
     'data[User][username]': credentials.login, 
     'data[User][password]': credentials.password 
    }; 
    conf.headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}; 


    var userData={ 
    'username':'mbah', 
    'password':'[email protected]', 
    'applicationid':'1' 
}; 

var deferred = $q.defer(); 

$http.post('auth/path/UserLogin/Login',userData).success(function(data){ 
     console.log(data); 
     if(!angular.isDefined(data.status)){ 

        deferred.resolve($http.post(url_+'/users/login',$.param(p),conf)); 
     } 
     else{ 
      deferred.reject(); 
     } 
}) 

return deferred.promise; 

}; 

那是我最后的代码和它作为expected.Thanks工作帮助球员