I'm working on a app based on AngularJS. I would like to load some contents that are available on a web site server. On a web browser, it uses a really basic form to login. I test how the server reacts with a nodeJS server using request module and succeeded to load what I wanted. Now I would like to do this with AngularJS.
I have to :
create a post request which contains the form. The server would answer with a request containing a redirection to the urlPortail and a set-cookie.
block this redirection or at least put the cookie asked to the headers of the redirection request
put this cookie to new requests to load the content I want
What I writed :
$http({
method: 'POST',
url: 'url/login.php',
data: $.param({login: 'login', password: 'password'}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.success(function(data1, status, headers1) {
console.log('headers 1')
console.log(headers1())
var session = parseCookies(headers1()); //Create a string (something like 'id=12jkl23244') with set-cookie of the headers
$http({
method : 'GET',
url: 'url/urlIWant',
headers: {cookie: session}
})
.success(function(data2, status, headers2){
console.log(data2)
})
This code doesn't work.
data1 contains the html of 'login.php' as if AngularJS made the redirection to 'urlPortail' without putting the cookie session of set-cookie (so the server doesn't know who I am and answers with the default page 'login.php') or worse, this stratagem to put a form in a request doesn't work.
data2 also contains the html of 'login.php'.
I'm blocking on this for a really really long time. I heard about $cookies and $storeCookies but I'm not understanding well what they do. Especially as they seem to work with the browser whereas I'm looking to put all this AngularJS stuff to an Ionic project,which is a framework to create hybrid Mobile Apps.
Hope you could help me. All comments are welcome. Thanks in advance.
I finally found the answer several days later. It handles everything alone. I just have to put the requests in the right order and all the cookies are automatically managed.