从AJAX POST响应获取并存储Cookie(从Set-Cookie) [英] Get and store cookie (from Set-Cookie) from an AJAX POST response

查看:9166
本文介绍了从AJAX POST响应获取并存储Cookie(从Set-Cookie)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的jQuery AJAX POST代码:

I have a simple jQuery AJAX POST code:

$.ajax({
    type: "POST",
    url: AppConstants.URLs.PROXY,
    data: message,
    xhrFields: {
        withCredentials: true
    },
    success: function(data, status, xhr) {
        console.log("Cookie: " + xhr.getResponseHeader("Set-Cookie"));
    }
});

我希望获得cookie并使用

and I wish to get the cookie and save it using cookies-js.

但是根据 cookies-js 。 //www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-methodrel =nofollow> http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28% 29方法:

But according to http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-method:



  1. 返回所有响应标头不包括对Set-Cookie或Set-Cookie2 的不区分大小写的标头作为单个字符串,每个标题行由U + 000D CR U + 000A LF对(不包括状态行)分隔,以及每个标题名称和标题值由U + 003A COLON U + 0020 SPACE对分隔。

  1. Return all response headers, excluding headers that are a case-insensitive match for Set-Cookie or Set-Cookie2, as a single string, with each header line separated by a U+000D CR U+000A LF pair, excluding the status line, and with each header name and header value separated by a U+003A COLON U+0020 SPACE pair.


使用Chrome中的网络工具,响应头中显示Set-Cookie。我还验证了使用 curl 出现的Set-Cookie标题。

Using the Network tool in Chrome, "Set-Cookie" is visible in the Response headers. I also verified that the "Set-Cookie" header appears using curl.

保存cookie在我的前端应用程序?此外,我的应用程式只能在 https 上执行。

What do I have to do to save the cookie in my front end app? Also, my app is running on https only.

我很乐意根据要求提供更多详情。

I'd gladly provide more details upon request.

推荐答案

您无法在JS中获取Cookie数据。

You can't get the cookie data in your JS. The API won't allow you.


我需要做什么才能将Cookie储存在我的前端应用程式中?

What do I have to do to save the cookie in my front end app?

只需设置从服务器端代码发送 Set-Cookie 头。浏览器应自动保存。

Just set send the Set-Cookie header from the server side code. The browser should save it automatically.

这篇关于从AJAX POST响应获取并存储Cookie(从Set-Cookie)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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