【问题标题】:Jquery Ajax request not saving cookiesJquery Ajax请求不保存cookie
【发布时间】:2021-08-15 21:02:06
【问题描述】:

我正在使用 jquery 从 domain1 向 domain2 发送 ajax 请求。

代码如下:

let settings = {
        url: "https://domain2.com",
        method: "POST",
        headers: {
            "Access-Control-Allow-Credentials": true,
            "Access-Control-Allow-Origin": "https://domain1.com",
        },
        contentType: "application/json",
        data: JSON.stringify(data),
        xhrFields: {
            withCredentials: true
        }
    };
    jQuery.ajax(settings)
    .then( res => {
       // Do something with result
    );

但在我检查浏览器 cookie 时请求后,响应 cookie 不存在。 我还检查了 domain2 发送 cookie 的网络选项卡。

谁知道问题出在哪里?

【问题讨论】:

    标签: jquery cookies cross-domain samesite


    【解决方案1】:

    在 ajax 设置中使用:

    let settings = {
       url: "https://domain2.com",
       crossDomain: true,
       ... your settings
    }
    

    【讨论】:

      【解决方案2】:

      我发现了问题,这是我的错。

      以上所有设置都是正确的,问题是关于设置 cookie 的 domain2

      具有SameSite = strict 属性,因此 domian1 无法设置这些 cookie。

      更多信息:SameSite Cookies

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 1970-01-01
        • 2015-01-12
        • 1970-01-01
        • 2011-03-10
        • 2012-02-09
        • 2020-06-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多