Why is the Cookie Header not set on my same-domain $.ajax request within phantomjs?

半世苍凉 提交于 2019-12-10 19:34:55

问题


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

来源:https://stackoverflow.com/questions/23679287/why-is-the-cookie-header-not-set-on-my-same-domain-ajax-request-within-phantom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!