如何从 AJAX 响应中获取 cookie? [英] How to get a cookie from an AJAX response?
问题描述
我在同一个域上有一个 $.ajax
请求,我想读取 cookie.它不断返回 null
.
I have a $.ajax
request on the same domain and I want to read the cookie. It keeps returning null
.
$.ajax({
type: 'GET',
url: myUrl,
success: function(output, status, xhr) {
alert(xhr.getResponseHeader("MyCookie"));
},
cache: false
});
有什么想法吗?为此,我正在使用 Chrome.
Any ideas? I'm using Chrome for this.
推荐答案
您正在寻找 Set-Cookie
的响应头:
You're looking for a response header of Set-Cookie
:
xhr.getResponseHeader('Set-Cookie');
不过它不适用于 HTTPOnly cookie.
It won't work with HTTPOnly cookies though.
根据XMLHttpRequest Level 1 和 XMLHttpRequest Level 2,这特定的响应头属于禁止的"范围.您可以使用 getResponseHeader()
获取响应标头,因此它可以工作的唯一原因基本上是顽皮";浏览器.
According to the XMLHttpRequest Level 1 and XMLHttpRequest Level 2, this particular response headers falls under the "forbidden" response headers that you can obtain using getResponseHeader()
, so the only reason why this could work is basically a "naughty" browser.
这篇关于如何从 AJAX 响应中获取 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!