Kee*_*ker 40 html ajax jquery google-chrome
我$.ajax在同一个域上有一个请求,我想读取cookie.它不断返回null.
$.ajax({
type: 'GET',
url: myUrl,
success: function(output, status, xhr) {
alert(xhr.getResponseHeader("MyCookie"));
},
cache: false
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我正在使用Chrome.
MrE*_*MrE 44
该浏览器不能给访问的第三方Cookie,像那些从出于安全原因,Ajax请求接受,但是它会自动为您需要的照顾!
为此,您需要:
1)使用ajax请求登录,您希望从中返回cookie:
$.ajax("https://example.com/v2/login", {
method: 'POST',
data: {login_id: user, password: password},
crossDomain: true,
success: login_success,
error: login_error
});
Run Code Online (Sandbox Code Playgroud)
2)xhrFields: { withCredentials: true }在下一个ajax请求中连接以使用浏览器保存的凭据
$.ajax("https://example.com/v2/whatever", {
method: 'GET',
xhrFields: { withCredentials: true },
crossDomain: true,
success: whatever_success,
error: whatever_error
});
Run Code Online (Sandbox Code Playgroud)
浏览器会为您处理这些cookie,即使它们不是可读的headers也不是可读的document.cookie
Ja͢*_*͢ck 39
您正在寻找以下的响应标头Set-Cookie:
xhr.getResponseHeader('Set-Cookie');
Run Code Online (Sandbox Code Playgroud)
但它不适用于HTTPOnly cookie.
根据XMLHttpRequest Level 1和XMLHttpRequest Level 2,这个特定的响应头部属于你可以使用的"禁止"响应头getResponseHeader(),所以这个可行的唯一原因基本上是一个"顽皮"的浏览器.