Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待在location.href重定向之前设置CORS cookie_Javascript_Jquery_Cookies_Browser_Cross Domain - Fatal编程技术网

Javascript 等待在location.href重定向之前设置CORS cookie

Javascript 等待在location.href重定向之前设置CORS cookie,javascript,jquery,cookies,browser,cross-domain,Javascript,Jquery,Cookies,Browser,Cross Domain,我正在尝试使登录在多个域(我控制的域)中持续存在。我的策略是,在成功的登录响应之后,我发送一个CORS请求,以获取其他每个必需域的cookie。返回cookies后,我重定向到用户主页(可能在任何域上) 如果我注释掉重定向,我发现一切都正常:所有CORS cookies都已设置,并且我已登录到其他域。但是,当我重定向时,CORS cookies有时没有设置。以下是我的客户端代码的总体思路: // This is running client side when the user visi

我正在尝试使登录在多个域(我控制的域)中持续存在。我的策略是,在成功的登录响应之后,我发送一个CORS请求,以获取其他每个必需域的cookie。返回cookies后,我重定向到用户主页(可能在任何域上)

如果我注释掉重定向,我发现一切都正常:所有CORS cookies都已设置,并且我已登录到其他域。但是,当我重定向时,CORS cookies有时没有设置。以下是我的客户端代码的总体思路:

    // This is running client side when the user visits "A.com/index.html"
    // PLEASE NOTE: the distinction between A.com and B.com is important

     $.post('https://A.com/attemptLogin', function(data) {
        if (!data.success) { return; }
        var token = data.loginToken;
        var userURL = data.userURL; // userURL may be in A.com or B.com...
        // now get a cookie at the alternate domain B.com
        $.ajax({
            url: 'http://B.com/getDomainCookie/' + token,
            method: 'GET',
            xhrFields: { withCredentials: true },
            success: function() {
                // if I comment out the next line, the cookie is always set successfully
                // if I leave the line in, it is hit or miss whether the cookie is set.
                location.href = userURL;
            }
        });
    });
我猜想ajax回调是在浏览器完成cookie设置之前触发的,这可能与它是一个B.com cookie这一事实有关,而这段代码是在a.com上运行的


感谢您的帮助。这是我发布的第一个问题,因此对问题ettiquette和格式的建设性批评也非常感谢

我在window.setTimeout(function(){location.href='';},50)中设置了50毫秒的延迟
给它一点空闲时间来存储cookie。另一方面,在匿名窗口中似乎根本不起作用,丢弃了所有的CORS cookies。

只是为了澄清一下,对
的GET请求http://B.com/getDomainCookie/“+token
应该设置cookie然后返回吗?如果这是您想要的,那么我认为您的问题在B端,异步函数设置cookie并返回响应。有可能吗?@cventr,是的,访问B.com纯粹是为了设置cookie。返回仅包含OK响应和B.com域的cookie。至于B端的异步,我不确定你的意思。。。我控制B.com/getDomainCookie另一端的服务器,它肯定会返回我想要的响应。好的,我看不到B端有什么,所以我只是想猜测:你确定在设置cookie后立即返回响应吗?因为您使用的函数可能是异步的,并且在实际设置cookies@cventr,我觉得不是这样,因为如果我留在页面上,cookie总是设置好的。但为了更全面,我可以告诉您/getDomainCookie后面的(节点表达样式)端点以两行结尾:
res.cookie('name','value');返回res.send(200)逐行逐行设置,因此应始终设置cookie。如果cookie在返回200状态之前写入成功,您不能在B侧进行测试吗?