简体   繁体   English

是否可以从JavaScript中的HTTP响应头获取Set-Cookie值?

[英]Is it possible to get the Set-Cookie value from an HTTP response header in JavaScript?

I'm using jQuery's ajax() method to make some asynchronous server calls and want to catch the case where a call fails because the session has timed out. 我正在使用jQuery的ajax()方法来进行一些异步服务器调用,并希望捕获调用失败的情况,因为会话已经超时。

From looking at the response headers in this case, I see that they include 从这个案例中查看响应头文件,我看到它们包括

Set-Cookie: SMSESSION=LOGGEDOFF

which seems like a pretty reliable test. 这似乎是一个非常可靠的测试。 But calling getAllResponseHeaders on the XMLHttpRequest object passed to jQuery's error callback apparently returns an empty string, and I'm having trouble figuring out any other way of getting that header information. 但是在传递给jQuery的error回调的XMLHttpRequest对象上调用getAllResponseHeaders显然会返回一个空字符串,而我无法找到获取该头信息的任何其他方法。

Is this possible? 这可能吗?

If you read the W3 XHR spec you'll see that they don't allow you to access the set-cookie response header via a getAllResponseHeaders('Set-Cookie') call. 如果您阅读W3 XHR规范,您将看到它们不允许您通过getAllResponseHeaders('Set-Cookie')调用访问set-cookie响应头。

See 4.7.3 The getResponseHeader() method: 请参见4.7.3 getResponseHeader()方法:

Bullet point 3: "If header is a case-insensitive match for Set-Cookie or Set-Cookie2, return null." 要点3: “如果标头是Set-Cookie或Set-Cookie2的不区分大小写的匹配,则返回null。”

http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-method http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-method

jfriend00 also left this response in the comments above but I thought this question could use a legitimate answer. jfriend00也在上面的评论中留下了这个回复,但我认为这个问题可以使用合法的答案。

如果文档来自同一个域和路径,请使用document.cookie接口如果cookie具有http-only属性集,则无法访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM