【问题标题】:Who took the cookie from the AJAX call?谁从 AJAX 调用中获取了 cookie?
【发布时间】:2012-09-24 03:54:13
【问题描述】:

当我通过 AJAX 调用 URL 时,它会读取哪些 cookie?会话也是如此。

谁实际运行线程?它是我的默认系统浏览器、当前浏览器还是其他实体?

【问题讨论】:

    标签: ajax session cookies


    【解决方案1】:

    这可能会令人困惑,因为这一切都发生在“幕后”。我发现,与任何其他 Javascript 一样,加载页面的浏览器是运行 AJAX 代码的浏览器。此外 - 浏览器的相同实例,这意味着会话也可以使用。


    我已经运行了一些代码来得出这个结论。以下示例使用经典 ASP。

    首先,我有一个写入这些变量的文件:

    Response.Cookies("testing") = "One, Two, Three"
    Session("testing") = "Forty One, Forty Two, Forty Three"
    

    接下来,读取一个文件(并显示结果):

    Cookie is: 
    <%
    Response.Write Request.Cookies("testing")
    %>
    <br>
    Session is:
    <%
    Response.Write Session("testing")
    %>
    

    最后,AJAXly 调用一个文件:

    <div id="result"></div>
    <script type="text/javascript">
    //Different browsers initiate ajax differently
    try {var oXH = eval("new Active"+"X"+"Object('MSXML2.XMLHTTP')");}
    catch(e) {var oXH = new XMLHttpRequest();}
    
    //Call page that reads the cookie
    oXH.open("GET","/testCookie.asp",true);
    oXH.onreadystatechange = function(){
        if ((oXH.readyState != 4)||(oXH.status != 200))
            return true;
        else
        {
            document.getElementById("result").innerHTML = oXH.responseText;
        }
    };
    oXH.send(null);
    </script>
    

    在一个浏览器上运行第一个文件,在几个浏览器上运行最后一个文件,第一个显示:

    Cookie is: One, Two, Three
    Session is: Forty One, Forty Two, Forty Three
    

    其余展示:

    Cookie is:
    Session is:
    

    所以你有它:)

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 2014-08-23
      • 2012-10-02
      • 2014-01-09
      • 2011-09-28
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多