Can jQuery.getJSON put a domain's cookies in the header of the request it makes?

前端 未结 2 694
無奈伤痛
無奈伤痛 2021-01-20 18:35

(Note: See also the related question Can browsers react to Set-Cookie specified in headers in an XSS jquery.getJSON() request?)

I can\'t seem to set a cookie

相关标签:
2条回答
  • 2021-01-20 19:19

    Are you running all of your tests through localhost? Are you using IE? If so it will be enforcing its own special brand of security requirements and likely dumping your cookies. Open fiddler and use http://ipv4.fiddler to bypass that.

    If that type of trickery is not going on (as it appears you are using FireFox) , it may also be the case that you do need to explicitely set the cookie's domain to be the same as the domain of your JSON request. A browser won't send cookies set for domain A to a request to domain B. I am not 100% sure this is the case though.

    0 讨论(0)
  • 2021-01-20 19:25

    You cannot set a cross-domain cookie, because that would open the browser (and therefore the user) to XSS attacks.

    To quote from the QuirksMode.org article that I reference above:

    Please note that the purpose of the domain is to allow cookies to cross sub-domains. My cookie will not be read by search.quirksmode.org because its domain is www.quirksmode.org . When I set the domain to quirksmode.org, the search sub-domain may also read the cookie. I cannot set the cookie domain to a domain I'm not in, I cannot make the domain www.microsoft.com . Only quirksmode.org is allowed, in this case.

    If you want to make cross-site request with cookie values you will need to set up a special proxy on a server you control that will let you pass in values to be sent as cookie values (probably via POST parameters). You'll also want to make sure that you properly secure it, lest your proxy become the means by which someone else's private information is "liberated".

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