ajax传cookie,为什么jquery的.ajax()方法没有发送我的会话cookie?

我在跨域方案中运行。 在登录期间,远程服务器返回Set-Cookie标头以及设置为true的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: *。

对远程服务器的下一个ajax调用应该使用此cookie。

CORS的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: *允许跨域日志记录。 有关示例,请查看[https://developer.mozilla.org/En/HTTP_access_control]。

对我来说,它似乎是JQuery中的一个错误(或者至少是下一版本中的功能)。

更新:

不会自动从AJAX响应中设置Cookie(引用:[http://aleembawany.com/2006/11/14/anatomy-of-a-well-designed-ajax-login-experience/)]

为什么?

您无法从响应中获取cookie的值以手动设置([http://www.w3.org/TR/XMLHttpRequest/#dom-xmlhttprequest-getresponseheader)]

我糊涂了..

应该有一种方法可以要求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: *设置XMLHttpRequest.withCredentials = "true"参数。

回答:您应该使用[http://api.jquery.com/jQuery.ajax/]的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: *参数

文档中的示例是:

$.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.example

Cookie: pageAccess=2

服务器应该响应:

Access-Control-Allow-Origin: http://foo.example

Access-Control-Allow-Credentials: true

[payload]

否则,有效负载将不会返回到脚本。 请参阅:[https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Requests_with_credentials]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值