gpt4 book ai didi

javascript - 从 AJAX POST 响应获取和存储 cookie(来自 Set-Cookie)

转载 作者:数据小太阳 更新时间:2023-10-29 04:02:23 25 4
gpt4 key购买 nike

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

$.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 并使用 cookies-js 保存它.

但根据http://www.w3.org/TR/XMLHttpRequest/#the-getallresponseheaders%28%29-method :

  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”在响应 header 中可见。我还使用 curl 验证了“Set-Cookie” header 出现。

我必须做什么才能将 cookie 保存在我的前端应用程序中?此外,我的应用仅在 https 上运行。

我很乐意根据要求提供更多详细信息。

最佳答案

您无法在您的 JS 中获取 cookie 数据。 API 不允许。

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

只需在服务器端代码的响应中设置 Set-Cookie header 。浏览器应该会自动保存它。

作为开发人员,您可以使用“开发人员工具”检查 cookie 的值。

并且相同的 cookie 将在后续请求中发送到同一域,直到 cookie 过期。

关于javascript - 从 AJAX POST 响应获取和存储 cookie(来自 Set-Cookie),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com