user699681
user699681

Reputation: 213

Cookies are not set from Cross domain AJAX request

I am loading a javascript on my page http://vista.local/. The script sends a POST request to a webservice http://api.vista.local/sessions which sets the cookies is set in the response

Accept-Ranges:bytes
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Accept, Origin, Content-Type, Cookie
Access-Control-Allow-Methods:GET,POST
Access-Control-Allow-Origin:http://vista.local
Age:0
Cache-Control:public, max-age=1
Connection:keep-alive
Content-Length:36
Content-Type:application/json
Date:Sun, 23 Aug 2015 06:00:45 GMT
Server:Apache-Coyote/1.1
Set-Cookie:vista-session=c07c4d25-5c6e-47a3-9bbc-60f7d122075a;Domain=.vista.local;Path=/;HttpOnly
Set-Cookie:vista-loggedin=true;Domain=.vista.local;Path=/;
Set-Cookie:[email protected];Domain=.vista.local;Path=/;HttpOnly
Set-Cookie:vista-session-dummy=c07c4d25-5c6e-47a3-9bbc-60f7d122075a;Version=1
Set-Cookie:vista-secured-session=08239195-e95e-4f42-adeb-5d9c745ca853;Domain=.vista.local;Path=/;Secure;HttpOnly
Vary:Accept-Encoding,User-Agent
X-UA:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36

I tried different types of cookies.. HttpOnly, non-HttpOnly without domain, with domain. Nothing works.

My JS/JQuery code to request

function login(formdata) {
    $.ajax({
        url : api.sessions,
        method : "POST",
        data : JSON.stringify(formdata),
        dataType : 'text',
        contentType: 'application/json',
        success : function(data) {
            console.log("yeeeii")
        },
        error: function(jqXHR, textStatus, errorThrown) {
            $(".error").html(textStatus);
        }
    });
}

Upvotes: 3

Views: 7366

Answers (1)

epascarello
epascarello

Reputation: 207501

Set the withCredentials

...
method : "POST",
xhrFields: {
    withCredentials: true
},
...

Upvotes: 9

Related Questions