17

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?

alessioalex
  • 62,577
  • 16
  • 155
  • 122
Michael Dausmann
  • 4,202
  • 3
  • 35
  • 48
  • I don't think that PhoneGap supports cookies at all. http://stackoverflow.com/questions/3709315/phonegap-cookie-based-authentication-php-not-working-webview http://stackoverflow.com/questions/6968209/phonegap-javascript-app-how-can-i-store-cookies – Luka Feb 15 '12 at 00:40

2 Answers2

1

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.

Scorpius
  • 999
  • 1
  • 10
  • 22
0

Phonegap does not support cookie abstraction. Never really needed to as there are already apps/plug-ins that do. Plus it is intended to wrap up the functionality of the phone/device, not the browser. You CAN however do this with a jQuery plug-in.

https://github.com/carhartl/jquery-cookie

Drewness
  • 5,004
  • 4
  • 32
  • 50