【问题标题】:ajax http request not sending document.cookie on same domainajax http请求不在同一个域上发送document.cookie
【发布时间】:2019-10-28 14:34:36
【问题描述】:

为什么我的 ajax 请求没有发送 document.cookie?我在同一个域!

这是我的代码:

document.cookie = "test=test";
  var query_url = 'http://example.com/dosomething';
    $.ajax({
        url: query_url,
        type: 'GET',
        xhrFields: {withCredentials: true},
        success: function(data){
            if(data === "OK") {
                //some code
            };

            if(data !== "OK") {
                //some code
            }

        },
        error: function(error){
                            console.log("===ERROR");
                            console.log(error);
                }, 

      });

【问题讨论】:

    标签: ajax cookies request


    【解决方案1】:

    在为我的 document.cookie 设置路径后,我的 ajax 请求正在发送! xhr 字段不是必需的。

    document.cookie = "test=test; path=/";
    

    (编辑): 我的前端 URL 是 example.com/fe/app.html。通过在 example.com/ 上运行后端,也许这就是我在 cookie 中需要“path=/”的原因

    【讨论】:

      猜你喜欢
      • 2014-11-24
      • 2012-10-08
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      相关资源
      最近更新 更多