Read/Write the cookies from an XHR request/response (PhoneGap/Cordova)

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).

Answers


I confirm that cookies are not manageable from JavaScript's XMLHttpRequest object in PhoneGap/Cordova on Android, regardless of the framework used (so not an angular issue). It seems to be a feature, not a bug, with no plans to expose the cookies down to the JavaScript side (the cookies are managed but just not exposed to the JavaScript client-side of Cordova). There seem to be a workaround on iOS but I cannot confirm.

The only workaround I found for Android is to create a Cordova plugin that performs the HTTP calls natively (using java.net.* on Android), giving access to the cookies and passing them back to the JavaScript side. Check Cordova plugins documentation for more details: http://docs.phonegap.com/en/4.0.0/guide_hybrid_plugins_index.md.html

Luckily, Cordova plugins are quite easy to setup and very powerful as you get all the natively possible features.


Need Your Help

How do I get the YouTube video ID from a URL?

javascript regex url youtube

I want to get the v=id from YouTube’s URL with JavaScript (no jQuery, pure JavaScript).

The M of MVC (ZF)

php zend-framework

I don't get it how it really works.