【问题标题】:Set-Cookie header not setting cookie in ChromeSet-Cookie 标头未在 Chrome 中设置 cookie
【发布时间】:2015-09-12 13:02:52
【问题描述】:

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

但是,虽然响应标头包含“Set-Cookie”标头,但实际上并未设置任何 cookie。我正在使用谷歌浏览器。

这是响应标头:

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。任何和所有的帮助将不胜感激!

【问题讨论】:

标签: javascript jquery ajax google-chrome cookies


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 1970-01-01
    • 2019-09-08
    相关资源
    最近更新 更多