简体   繁体   English

Set-Cookie标头未在Chrome中设置Cookie

[英]Set-Cookie header not setting cookie in Chrome

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. 我是AJAXing调用另一个服务API,然后应该返回一个cookie,该cookie将在我的浏览器中设置,以允许我进行其余的API调用。

However, while the response headers include a 'Set-Cookie' header, no cookie is ever actually set. 但是,虽然响应标头包含“Set-Cookie”标头,但实际上并未设置cookie。 I'm using Google Chrome. 我正在使用Google Chrome。

Here is the Response Headers: 这是响应标题:

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

And here is the AJAX call: 这是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);
            }
});

The Chrome resources page also shows that no cookie is being set. Chrome资源页面还显示没有设置Cookie。 Any and all help would be greatly appreciated! 任何和所有的帮助将不胜感激!

You need to append withCredentials to your XHR call, this answer shows how to do that with jQuery. 您需要将withCredentials附加到XHR调用,此答案显示了如何使用jQuery执行此操作。 https://stackoverflow.com/a/7190487 Without that additional flag, the browser will not accept a set-cookie header. https://stackoverflow.com/a/7190487如果没有该附加标志,浏览器将不接受set-cookie标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM