Why is the Cookie Header not set on my same-domain

2019-07-07 02:44发布

问题:

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:

  1. Domain is opened in the browser (no cookies yet)
  2. Login via $.ajax xhr Request (response has cookies)
  3. Requests via $.ajax to pages I want to see/modify/ect. (requests miss Cookie header)
  4. GO TO 3. || GO TO 5.
  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