jQuery 如何从 AJAX 响应中获取 cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12840410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a cookie from an AJAX response?
提问by Kees C. Bakker
I have a $.ajax
request on the same domain and I want to read the cookie. It keeps returning null
.
我$.ajax
在同一个域上有一个请求,我想读取 cookie。它不断返回null
。
$.ajax({
type: 'GET',
url: myUrl,
success: function(output, status, xhr) {
alert(xhr.getResponseHeader("MyCookie"));
},
cache: false
});
Any ideas? I'm using Chrome for this.
有任何想法吗?为此,我正在使用 Chrome。
采纳答案by Ja?ck
You're looking for a response header of Set-Cookie
:
您正在寻找以下响应标头Set-Cookie
:
xhr.getResponseHeader('Set-Cookie');
It won't work with HTTPOnly cookies though.
但是它不适用于 HTTPOnly cookie。
Update
更新
According to the XMLHttpRequest Level 1and 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.
根据XMLHttpRequest Level 1和XMLHttpRequest Level 2,这个特定的响应头属于您可以使用 获取的“禁止”响应头getResponseHeader()
,所以它可以工作的唯一原因基本上是一个“顽皮”的浏览器。
回答by MrE
The browsercannot give access to 3rd party cookies like those received from ajax requests for security reasons, howeverit takes care of those automatically for you!
该浏览器不能给访问的第三方Cookie,像那些从出于安全原因,Ajax请求接受,但是它会自动为您需要的照顾!
For this to work you need to:
为此,您需要:
1) login with the ajax request from which you expect cookies to be returned:
1) 使用您希望返回 cookie 的 ajax 请求登录:
$.ajax("https://example.com/v2/login", {
method: 'POST',
data: {login_id: user, password: password},
crossDomain: true,
success: login_success,
error: login_error
});
2) Connect with xhrFields: { withCredentials: true }
in the next ajax request(s) to use the credentials saved by the browser
2)xhrFields: { withCredentials: true }
在下一个 ajax 请求中连接以使用浏览器保存的凭据
$.ajax("https://example.com/v2/whatever", {
method: 'GET',
xhrFields: { withCredentials: true },
crossDomain: true,
success: whatever_success,
error: whatever_error
});
The browser takes care of these cookies for you even though they are not readable from the headers
nor the document.cookie
浏览器需要照顾这些cookie的你,即使他们无法从读取headers
,也不是document.cookie
回答by alexis
xhr.getResponseHeader('Set-Cookie');
It won't work for me.
它对我不起作用。
I use this
我用这个
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
}
return "";
}
success: function(output, status, xhr) {
alert(getCookie("MyCookie"));
},
回答by cpoole
Similar to yebmouxing I could not the
类似于 yebmouxing 我不能
xhr.getResponseHeader('Set-Cookie');
method to work. It would only return null even if I had set HTTPOnly to false on my server.
工作的方法。即使我在服务器上将 HTTPOnly 设置为 false,它也只会返回 null。
I too wrote a simple js helper function to grab the cookies from the document. This function is very basic and only works if you know the additional info (lifespan, domain, path, etc. etc.) to add yourself:
我也写了一个简单的 js 辅助函数来从文档中获取 cookie。此功能非常基础,仅当您知道要添加自己的其他信息(寿命、域、路径等)时才有效:
function getCookie(cookieName){
var cookieArray = document.cookie.split(';');
for(var i=0; i<cookieArray.length; i++){
var cookie = cookieArray[i];
while (cookie.charAt(0)==' '){
cookie = cookie.substring(1);
}
cookieHalves = cookie.split('=');
if(cookieHalves[0]== cookieName){
return cookieHalves[1];
}
}
return "";
}