Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap, cookies, cross domain requests

I'm developing a phonegap application that needs to connect to a staless remote server. Some of the remote requests are authenticated and I was planning to use a remember-me service. So from the application there is a login option that is translated into the following request executed with jQuery $.ajax. Notice that all the requests are application/json

POST remoteserver/api/users/login

and the server responds setting a cookie

Set-Cookie REMEMBER_ME_COOKIE=amVzdS5hbG9uc29AZ21haWwuY29tOjEzNTEwMDg1NTg1NDY6ODdkOGFhNzEzY2NiZTRhZTIwZjZlNzgyY2E1ZmQyODY; Expires=Tue, 23-Oct-2012 16:09:18 GMT; Path=/; HttpOnly

In order to make cross domain requests work, the server responds as this for OPTIONS requests:

Access-Control-Allow-Headers : Content-Type, *
Access-Control-Allow-Origin : *
Access-Control-Max-Age : 1000

However the next request to the server is not setting the Cookie header.

1) Is it possible to make cookies work?

2) I have also tried to add the cookies manually in the $.ajax request, but it does not work either. Is it something wrong?

$.ajax({
        type : type,
        url : url,
        data : data,
        success : callback,
        dataType : dataType,
        contentType : contentType,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("Cookie", "REMEMBER_ME_COOKIE=amVzdS5hbG9uc29AZ21ha");
          },
        error : function(jqXHR, textStatus, errorThrown) {
            handleError(jqXHR, textStatus, errorThrown);
        }
    });
like image 624
jalogar Avatar asked Nov 13 '22 20:11

jalogar


1 Answers

Have you tried structuring the headers into your request?

$.ajax({
        type : type,
        url : url,
        data : data,
        success : callback,
        dataType : dataType,
        contentType : contentType,
        headers:{
             "Cookie" : "amVzdS5hbG9uc29AZ21ha"
        },
        error : function(jqXHR, textStatus, errorThrown) {
            handleError(jqXHR, textStatus, errorThrown);
        }
    });
like image 76
user1026361 Avatar answered Nov 15 '22 10:11

user1026361