慕森王
我是在跨域的情况下操作。在登录期间,远程服务器将返回set-cookie头以及Access-Control-Allow-Credentials设定为真。下一个对远程服务器的Ajax调用应该使用此cookie。CORS‘sAccess-Control-Allow-Credentials是否允许跨域日志记录。查帐https:/developer.mozilla.org/en/HTTP_Access_Control举个例子。对我来说,这似乎是JQuery中的一个bug(或者至少在下一个版本中是将要出现的特性)。最新情况:Cookie不会从Ajax响应中自动设置(引用:http:/aleembawany.com/2006/11/14/剖析-设计良好-ajax-登录-体验/)为什么?您无法从响应中获得cookie的值来手动设置它(http:/www.w3.org/tr/xmlHttpRequest/#dom-XMLHttpRequest-getresponseHeader)我很困惑.。应该有办法问jquery.ajax()设置XMLHttpRequest.withCredentials = "true"参数。答:你应该用xhrFields副词http://api.jquery.com/jQuery.ajax/文档中的示例如下:$.ajax({
url: a_cross_domain_url,
xhrFields: {
withCredentials: true
}});服务器正确地响应此请求也很重要。在这里复制@Frédéric和@Pebbl的精彩评论:Important note: when responding to a credentialed request, server must specify a domain, and cannot use wild carding. The above example would fail if the header was wildcarded as: Access-Control-Allow-Origin: *因此,当请求是:Origin: http://foo.exampleCookie: pageAccess=2服务器响应时应:Access-Control-Allow-Origin: http://foo.exampleAccess-Control-Allow-Credentials: true[payload]否则,负载将不会返回到脚本。见:https:/developer.mozilla.org/en-US/docs/web/HTTP/Access_Control_CORS#request_With_凭据