0

I have a server I'm trying to send cookies to. I send a first POST request, I store the received cookies from the response header and try with a second GET request to retrieve data an authenticated user could only access. The cross-domain request is okay, I'm able to retrieve my cookies within the Phonegap application, but when it comes to sending those, it fails. The request header does not contain any cookies. phonegap -v returns 5.1.1-0.29.0.

What I've already tried:

  • Add the domain to the whitelist

  • Make sure the cookies I saved were not in the request headers, server side, at all

  • Make the same request over C# to make sure it couldn't be a server side problem

The request code:

$.ajax(
        {
            type: "GET",
            url: "url",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Cookie", mycookies);
            },
            success: function(data){
                console.log(data);
            },
            error: function (xhr) {
                console.log(xhr.responseText);
            }
        }
    );

Any help would be very appreciated, thank you.

Johnny Bueti
  • 637
  • 1
  • 8
  • 27

3 Answers3

0

With quick search I understood Cordova doesn't hold cookies so you have to use Local Storage instead.

Here is example: http://justbuildsomething.com/cordova-and-express-session/

sharko
  • 382
  • 1
  • 13
0

Add cookie in headers ajax:

$.ajax(
    {
        type: "GET",
        url: "url",
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        headers: {
          Cookie: mycookies
        },
        success: function(data){
            console.log(data);
        },
        error: function (xhr) {
            console.log(xhr.responseText);
        }
    }
);
Hanh Le
  • 894
  • 6
  • 14
0

Eventually, Phonegap does not appear to have any external plugin that can let you execute cross-domain AJAX calls with a cookie header. Silly. I have solved by writing a little proxy, couldn't get around with any other better solution. Thanks to whoever tried to help, I appreciate it.

Johnny Bueti
  • 637
  • 1
  • 8
  • 27