2017-02-23 71 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工作幫助球員