Set-Cookie标头未在Chrome中设置Cookie [英] Set-Cookie header not setting cookie in Chrome

查看:424
本文介绍了Set-Cookie标头未在Chrome中设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在终止对另一个服务API的调用,然后应该返回一个cookie,该cookie将在我的浏览器中设置,以允许我进行其余的API调用.

I'm AJAXing a call to a another services API, which is then supposed to return a cookie that will be set in my browser to allow me to make the rest of my API calls.

但是,尽管响应标头包含"Set-Cookie"标头,但实际上并没有设置cookie.我正在使用谷歌浏览器.

However, while the response headers include a 'Set-Cookie' header, no cookie is ever actually set. I'm using Google Chrome.

以下是响应标题:

Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Encoding:gzip
Content-Length:37
Content-Type:application/json
Date:Thu, 25 Jun 2015 18:27:37 GMT
Expires:Thu, 25 Jun 2015 18:27:36 GMT
Server:nginx/1.4.6 (Ubuntu)
Set-Cookie:sessionid=67cb9796aa794a4975b28876ea6dd3d5; expires=Thu, 09-Jul-2015 18:27:37 GMT; httponly; Max-Age=1209600; Path=/
Vary:Cookie

这是AJAX调用:

$.ajax({
            type: "POST",
            crossDomain: true,
            contentType: 'text/plain',
            data: data,
            url: urlhere 
            success: function(result, status, xhr){
                console.log('hi');
                console.log(xhr.getAllResponseHeaders());
            }, 
            error: function(xhr){
                console.log(xhr.status);
                console.log(xhr.statusText);
            }
});

Chrome资源页面还显示未设置cookie.任何和所有帮助将不胜感激!

The Chrome resources page also shows that no cookie is being set. Any and all help would be greatly appreciated!

推荐答案

您需要将withCredentials附加到XHR调用中,此答案显示了如何使用jQuery来实现. https://stackoverflow.com/a/7190487 没有该附加标志,浏览器将不会接受set-cookie标头.

You need to append withCredentials to your XHR call, this answer shows how to do that with jQuery. https://stackoverflow.com/a/7190487 Without that additional flag, the browser will not accept a set-cookie header.

这篇关于Set-Cookie标头未在Chrome中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆