I’m running 2 sites on my local machine. (names configured via hosts file).

  • cf2.localhost.net (laravel)
  • cf2wp.localhost.net (wordpress)

When a user logs in to wordpress they get cookies from .localhost.net and when they load a page on the laravel site they receive this cookie data too.

When I try and do a jquery ajax request however the cookies aren’t sent

Using chrome tools I’ve verified that:

  • requesting http://cf2.localhost.net/home sends the wordpress cookies to the server
  • the cors preflight is returned ok with

    Access-Control-Allow-Credentials:true
    Access-Control-Allow-Headers:Authorization, Content-Type
    Access-Control-Allow-Methods:OPTIONS, GET, POST, PUT, PATCH, DELETE
    Access-Control-Allow-Origin:http://cf2.localhost.net
    Access-Control-Expose-Headers:X-WP-Total, X-WP-TotalPages
    Connection:Keep-Alive
    Content-Length:5166
    Content-Type:application/json; charset=UTF-8
    Date:Thu, 25 May 2017 06:30:36 GMT
    Keep-Alive:timeout=5, max=100
    Link:<http://cf2wp.localhost.net/wp-json/>; rel="https://api.w.org/"
    Server:Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/7.1.1
    Vary:Origin
    X-Content-Type-Options:nosniff
    X-Powered-By:PHP/7.1.1
    X-Robots-Tag:noindex
    
  • But the request that goes out next doesn’t have the wordpress cookies set. It only has the encrypted laravel ones from cf2.localhost.net

    Accept:application/json, text/javascript, */*; q=0.01
    Accept-Encoding:gzip, deflate, br
    Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
    Connection:keep-alive
    Content-Length:27
    Content-Type:application/json; charset=UTF-8
    Cookie:jenkins-timestamper-offset=-28800000; olfsk=olfsk19949676680174977; hblid=HFZEjdNeyuKRupkV3m39N0H8REA26ar9; _ga=GA1.1.1566562504.1495496019; io=qjc8cwjKxij4G9ggAAAA
    Host:localhost
    Origin:http://cf2.localhost.net
    Referer:http://cf2.localhost.net/home
    User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
    

Any idea what is going on?

Read more here: CORS issue with jquery and cookies for subdomains


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: