Why is jquery's .ajax() method not sending my session cookie?

后端 未结 11 1550
长情又很酷
长情又很酷 2020-11-22 02:25

After logging in via $.ajax() to a site, I am trying to send a second $.ajax() request to that site - but when I check the headers sent using FireB

11条回答
  •  后悔当初
    2020-11-22 03:03

    Using

    xhrFields: { withCredentials:true }
    

    as part of my jQuery ajax call was only part of the solution. I also needed to have the headers returned in the OPTIONS response from my resource:

    Access-Control-Allow-Origin : http://www.wombling.com
    Access-Control-Allow-Credentials : true
    

    It was important that only one allowed "origin" was in the response header of the OPTIONS call and not "*". I achieved this by reading the origin from the request and populating it back into the response - probably circumventing the original reason for the restriction, but in my use case the security is not paramount.

    I thought it worth explicitly mentioning the requirement for only one origin, as the W3C standard does allow for a space separated list -but Chrome doesn't! http://www.w3.org/TR/cors/#access-control-allow-origin-response-header NB the "in practice" bit.

提交回复
热议问题