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

I am using the jQuery ajax() method to make some asynchronous server calls and want to catch the case where the call failed because the session 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 in to jQuery error , the callback apparently returns an empty string, and I am unable to figure out any other way to get this header information.

Is it possible?

+6
source share
2 answers

If you read the W3 XHR specification, you will see that they do not allow you to access the set-cookie response header by calling getAllResponseHeaders('Set-Cookie') .

See 4.7.3 getResponseHeader () Method:

Boolean 3: "If the header is a case insensitive match for Set-Cookie or Set-Cookie2, return null."

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

jfriend00 also left this answer in the comments above, but I thought this question could use a legitimate answer.

0
source

If the document is from the same domain and path, use the document.cookie interface. If the cookie has only the http-only attribute, it is not available

-1
source

Source: https://habr.com/ru/post/895924/


All Articles