jquery ajax call not sending cookie

前端 未结 2 559
太阳男子
太阳男子 2020-12-10 09:11

I have a jQuery ajax call across a subdomain that works correctly except it\'s not sending the cookie. Is there any way to accomplish this?

相关标签:
2条回答
  • 2020-12-10 09:34

    This sounds like expected behavior to me. Cookies are per domain (and that includes subdomains). But I think you can force it with something like this:

    $.ajax({
       headers: {'Cookie' : document.cookie },
       url: "sub.domain.com",
       success: function(){ ...
    

    This is totally untested so let me know if it works ;)

    EDIT: There is an alternative solution available using:

    xhrFields: {
      withCredentials: true
    

    }

    Check here: How do I SET a Cookie (header) with XMLHttpRequest in JavaScript?.

    Also, you can set the cookies in php so that they are valid across all your subdomains. Something like this:

    ini_set('session.cookie_domain', '.example.com')
    

    Note the '.' before the domain - that will set the cookie for example.com and all its subdomains.

    You can set session.cookie_domain in your app using the above or set it in your php.ini.

    The above is stolen from here.

    0 讨论(0)
  • 2020-12-10 09:38

    Shouldn't this work if you use a CORS capable browser and set the withCredentials attribute?

    See HTTP Cookies and Ajax requests over HTTPS

    0 讨论(0)
提交回复
热议问题