Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Before using Angular.js's $http, I was testing the website's reactions with requests from the request module of node.js. My goal was to log in to a website with a POST request, take the cookie session and then download all the pages I wanted with requests with this cookie in their headers.

With Node.js, it looked like this :

request.post({url: myUrl/login.php, form:{login:'login', password:'password'}}, function(err, resp, body){
                var cookies = parseCookies(resp) //function that takes all the cookies 

                request({url: myUrl/portail, headers{cookie: cookies}}, function(err, resp, body){console.log('recu')})
            })

The response to post request is actually a redirection to myUrl/portail with 'set-cookie' in the headers

I would like to do this with my app using Angular.js, so I did this :

$http({
    method: 'POST',
    url: myUrl/login.php,
    data: $.param({login: $scope.loginData.login, password: $scope.loginData.password}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers) {
    var cookies = parseCookieAngularVersion(headers)
    $http({
      method:'GET',
      url: myUrl/portail,
      headers:{cookie:cookies}
    })
    .success(function(data, status, headers){
      console.log(headers())
    })
    .error(function(){
      console.log('ERRRROR')
    })
})
.error(function(data, status, headers, config) {
  console.log('ERRROR');
})

But it doesn't work... The headers don't contain any cookie and the server keeps send me the login page. The problem must be because AngularJs is a client-side application. Could someone helps me ? I would be very great. Thanks in advance

share|improve this question
    
(1) Make sure you have quotes in the url properties, i.e. url: "myUrl/login.php", and NOT url: myUrl/login.php,. (2) Make sure you are not violating any same-origin policies, i.e. the site that provides the code above is the same as the site you try to GET from. – Nikos Paraskevopoulos Sep 10 '14 at 11:14
    
All the url are exactly the same as those used in the Node.js code, which works fine. – GaétanZ Sep 10 '14 at 11:37
    
Now I success to have the cookie of the 'set-cookie' from the post request. Is the 'headers: {cookie: cookies}' (cookies is something like 'id=1234309jdsq') correct ? – GaétanZ Sep 10 '14 at 13:17
    
Or pass by $cookieStore necessary ? – GaétanZ Sep 10 '14 at 13:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.