I am using $http (angular) and posting to a distant website which I don't control.
When logging in, it gives me a 'site_session' cookie which I want to read.
Please note that I am able to call a remote website (cross-domain) because running on a mobile device, in a Cordova application.
I tried all sorts of solutions found on the web, without any luck.
I am using Angular 1.3.13 so the $cookies
after $timeout
is supposed to work, but doesn't.
I tried the withCredentials = true
trick, both in the $httpProvider.defaults and in the $http call's config itself: no luck.
$cookies seems to die and reset within each scope.
For example:
$http({
method: 'POST',
url: 'http://distantsite.com/login.php',
data: 'username=test&password=test',
headers: {
'Accept': 'text/html',
'Cookie': "site_session=abcd", // this will be blocked: refused to set unsafe header
'Content-Type': 'application/x-www-form-urlencoded',
'Access-Control-Allow-Credentials': true,
},
withCredentials: true,
}).success(function(response, status, headers){
$cookies.myTest1 = 'test1';
$log.info('cookies: ', $cookies); // Object {myTest1: "test1"}
$timeout(function(){
$cookies.myTest2 = 'test2';
$log.info('cookies after timeout: ', $cookies); // Object {myTest2: "test2"}, myTest1 has disappeared!
});
});
Outputs:
cookies: Object {myTest1: "test1"}
cookies after timeout: Object {myTest2: "test2"}
I don't get any of the real cookies, only the last one I set...
I'm completely out of solutions. Does anyone have a valid, working way to retrieve the cookies of a call to a remote (cross-domain) site from Angular in a Cordova appliation?
If possible, I'd like to keep $http for that, but I don't mind using something else as long as it works from client-side (browser).