Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable Third-Party Cookies under Phonegap and Android 3.2?

I am using $getJSON to hit a node.js endpoint under Phonegap and Android. The code looks like this

$.getJSON(
    serverURL + "/login?callback=?",
    "playerId=" + playerId + "&pwd=" + pwd,
    function(data){
        theCallbackFunction.call(null, JSON.parse(data));
    },
    function(jqXHR, textStatus, errorThrown) {
            alert('error ' + textStatus + " " + errorThrown);
    }
);  

In response to the login request, my server sends back a session cookie. This cookie is only accepted and returned in subsequent AJAX requests if 'Third-Party Cookies' are enabled in the browser. I have found that older Android devices (e.g. 2.2) allow this by default but new ones (3.2) do not.

Is it possible to force Phonegap to enable Third-Party Cookies for my Android application?

like image 644
Michael Dausmann Avatar asked Dec 02 '11 15:12

Michael Dausmann


People also ask

How do I enable 3rd party cookies in Chrome?

In the Privacy and Security section, select Content settings. In the Content Settings section, select Cookies. Make sure Allow sites to save and read cookie data (recommended) is toggled on. Make sure Block third-party cookies is toggled off.


1 Answers

I had a similar problem when trying to authenticate with my server. I instead resorted to the use of localStorage. See the code below or here.

    var store = window.localStorage,
request = {
    url: {SERVER_URL},
    headers : {
        Cookie: store.getItem('session')
    },
    complete: function (jqXHR, status){
        if (status != 'success') {
            console.log('ajax status: failure');
        } else if (store.getItem('session') != null) {
            console.log('ajax status: session exists');
        } else {
            console.log('ajax status: saving cookie');
            var header = jqXHR.getAllResponseHeaders();
            var match = header.match(/(Set-Cookie|set-cookie): (.+?);/);
            if (match) {
                session = match[2];
                store.setItem("session", session);
            }
        }
    }
}
$.ajax(request);

In the above, I'm checking for the localStorage variable 'session' and if it exists, it will send the stored cookie. If it doesn't exist, it will take the 'set-cookie' paramater sent in the headers by the server, match the pertinent part and store it in the 'session' variable of localStorage.

like image 144
Scorpius Avatar answered Nov 01 '22 05:11

Scorpius