admin管理员组文章数量:1391925
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.
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.
Is this possible?
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.
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.
Is this possible?
Share Improve this question asked Aug 25, 2011 at 21:30 Dan TaoDan Tao 129k57 gold badges308 silver badges450 bronze badges 9- 1 stackoverflow./questions/220231/… – quantumSoup Commented Aug 25, 2011 at 21:31
-
@quantumSoup: OK then, looks like it's a duplicate, so I will close this one... just to clarify, though, is the correct answer that you can't access the response headers? One of the answers to the other question mentions
getAllResponseHeaders
and has 17 upvotes. – Dan Tao Commented Aug 25, 2011 at 21:36 -
1
@zzzz: I had a similar thought... It seems that doesn't happen until after the callbacks have been called though; am I right in saying that? I suppose I could just do some sort of
setTimeout
deal in theplete
callback in that case... – Dan Tao Commented Aug 25, 2011 at 21:43 -
3
According to this spec (w3/TR/XMLHttpRequest/#the-getresponseheader-method),
getAllResponseHeaders()
will return all headers except theSet-Cookie
header. You have to go through the cookie interface to get cookies. – jfriend00 Commented Aug 25, 2011 at 22:34 -
1
@jfriend00: Sadly, what I'm seeing appears to be that: (1)
getAllResponseHeaders()
is returning""
(possibly jQuery is doing this?); and (2) the cookie in question isn't actually updated by the browser. I don't understand HTTP well enough to know why, though I can say the cookie is secure and hasn't expired. In any case, it's just frustrating that I can't get the value ofSet-Cookie
from the response header, as that's all I really care about (i.e., if the service even tries to set the cookie toLOGGEDOFF
, that's what I want to know). Do you happen to know if this is for security? – Dan Tao Commented Aug 26, 2011 at 1:28
2 Answers
Reset to default 2If 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.
See 4.7.3 The getResponseHeader() method:
Bullet point 3: "If header is a case-insensitive match for Set-Cookie or Set-Cookie2, return null."
http://www.w3/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-method
jfriend00 also left this response in the ments above but I thought this question could use a legitimate answer.
If the document is from the same domain and path, use the document.cookie interface If the cookie has the http-only attribute set, it's inaccessible
版权声明:本文标题:jquery - Is it possible to get the Set-Cookie value from an HTTP response header in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744732056a2622105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论