2017-02-23 1 views
3

Please find attached what I want to achieved관리하는 방법에 AngularJS와 HTTP 요청은

내가 이중 HTTP의 제 요구가 먼저 인증 요청을 만들고 싶어 진정한 다음 문을 실행하는 경우 (즉, 단지 $를 돌려 보내는 코드 조각을 호출 http 약속). angularJS에서 어떻게해야합니까? 현재로서는 정의되지 않은 상태로 돌아갑니다.

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; 
}; 

이런 식으로 함수는 용도에 사용할 수있는 약속을 반환합니다.

또는 내 최종 코드와 도움말들에 대한 expected.Thanks 등의 작업 그게 전부 당신에게

+0

도움 주셔서 감사합니다. 그러나, 나는 $ http.post (this.url + 'users/login', $. param (p), conf) 데이터를 넣고 싶습니다. 나는 그것을하는 또 다른 일반적인 방법이있다. – 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; 

}; 

도움이되지 않은 경우 더 자세히 설명해