I am issueing requests within the same domain via jQuery's $.ajax(request)
function. Unfortunately the Cookie header is never set (neither POST
nor GET
) and I don't know why.
If i understand jQuery Ajax correctly, the Cookie
header should be set according to the pages cookies (like document.cookie
). I've traced document.cookie
and to me it seems that it is updated correctly (according to the Set-Cookie
header) with every xhr response. But when debugging the Requests all have the Cookie
header not set.
Simplified version of how my requests are created:
var request = {
type: 'POST',
data: 'theDataIsCorrect=true',
async: false,
url: '/a/relative/url/like/this?maybe=even&with=this',
// withCredentials does not help (ignored on same domain request anyways)
xhrFields: { withCredentials: true },
beforeSend: function(jqXHR) { doStuff(); },
error: function(jqXHR, textStatus, errorThrown) { errorHandling(); },
complete: function(jqXHR, textStatus, errorThrown) { complete(); }
};
$.ajax(request);
How my scripts work:
- Domain is opened in the browser (no cookies yet)
- Login via
$.ajax
xhr Request (response has cookies) - Requests via
$.ajax
to pages I want to see/modify/ect. (requests missCookie
header) GO TO 3.
||GO TO 5.
- Logout via
$.ajax
EDIT:
I am using phantomjs
[1.9] as browser. This problem does not occur when using Firefox or Chrome. I've found this question, where someone is describing a similar behaviour: phantomjs - Cookie is not being sent for any XHR/POST/GET AJAX requests