跨跨域发送Cookie

时间:2015-03-11 19:57:40

标签: jquery cookies cross-domain cors

目标:跨域共享Cookie

我们的UI在端口P1的服务器“A”中运行,我们的服务在P2端口的同一服务器“A”中启动并运行。

 UI (Server 'A', Port P1) ----> Services (Server 'A', Port P2)

从UI到服务cookie的休息呼叫不会作为请求的一部分发送。

据我所知,Cookie不会在域之间共享。尝试了Why is jquery's .ajax() method not sending my session cookie?中提出的方法,但没有运气。

 $.ajax({
     url: a_cross_domain_url,
     xhrFields: {
     withCredentials: true
      }
    });

计划尝试以下方法,任何有关实现我们目标的意见或建议都将受到赞赏。感谢。

方法:

  1. 按照https://www.drupal.org/node/1133084

    中的建议在Cookie标头属性中设置所需的Cookie
    $.ajax({
     url: a_cross_domain_url,
     xhrFields: {
     withCredentials: true
      },
      //    headers: {'Cookie' : 'cookieName=cookieValue'},
      beforeSend: function(xhr) {
           xhr.setRequestHeader("Cookie", 'cookieName=cookieValue');
      });
    
  2. 使用公共VIP托管和访问用户界面和服务(例如:https://example.com),这样浏览器就不会感觉跨域提出请求。

     UI (https://example.com) ----> Services (https://example.com/service1)
    
  3. 更新:

    我们采用了第二种方法,因为这两个应用程序都被一个普通的VIP访问,所以cookie被浏览器发送过来。

1 个答案:

答案 0 :(得分:0)

我觉得第二种方法更清洁。我试过&它有效。

相关问题