【问题标题】:Javascript ajax GET call does not include cookie-sid on initial page loadJavascript ajax GET 调用在初始页面加载时不包括 cookie-sid
【发布时间】:2011-02-20 19:12:00
【问题描述】:

在我的应用程序中,当请求 /iframe 时,我创建一个 cookie 并提供 iframe.html 文件。在 html 文件中,我使用 Javascript Ajax 调用来请求用户数据 (/user),该调用在 $(document).ready 上调用,并且需要从 cookie 传递 sid(所以我知道调用已通过身份验证)。

问题在于,第一次请求 /iframe 时(当还没有 cookie 时),cookie-sid 没有与 Ajax get 调用一起传递。重新加载后,cookie-sid 通过 /user 调用传递。

有人对修复初始负载有什么建议吗?

我知道浏览器接收并存储 cookie,然后在每次请求新页面时将其发送回网站,但这是否也计入初始 Ajax 调用?

【问题讨论】:

    标签: javascript html ajax cookies node.js


    【解决方案1】:

    默认情况下,“凭据”例如 Cookie 和 HTTP 身份验证信息是 未使用跨站点请求发送 XMLHttpRequest。为了送他们, 你必须设置 withCredentials XMLHttpRequest 对象的属性。

    http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/

    示例

    var request = new XMLHttpRequest();
    var url = 'http://bar.other/resources/credentialed-content/';
    function callOtherDomain(){
      if(request)
      {
       request.open('GET', url, true);
       request.withCredentials = "true";
       request.onreadystatechange = handler;
       request.send();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多