<tfoot id='rrP50'></tfoot>
    1. <small id='rrP50'></small><noframes id='rrP50'>

      <i id='rrP50'><tr id='rrP50'><dt id='rrP50'><q id='rrP50'><span id='rrP50'><b id='rrP50'><form id='rrP50'><ins id='rrP50'></ins><ul id='rrP50'></ul><sub id='rrP50'></sub></form><legend id='rrP50'></legend><bdo id='rrP50'><pre id='rrP50'><center id='rrP50'></center></pre></bdo></b><th id='rrP50'></th></span></q></dt></tr></i><div id='rrP50'><tfoot id='rrP50'></tfoot><dl id='rrP50'><fieldset id='rrP50'></fieldset></dl></div>

    2. <legend id='rrP50'><style id='rrP50'><dir id='rrP50'><q id='rrP50'></q></dir></style></legend>
        <bdo id='rrP50'></bdo><ul id='rrP50'></ul>

      如何从 Ajax 调用修改 Cookie

      时间:2023-10-14
          <tbody id='c8UaN'></tbody>
          <bdo id='c8UaN'></bdo><ul id='c8UaN'></ul>

            • <i id='c8UaN'><tr id='c8UaN'><dt id='c8UaN'><q id='c8UaN'><span id='c8UaN'><b id='c8UaN'><form id='c8UaN'><ins id='c8UaN'></ins><ul id='c8UaN'></ul><sub id='c8UaN'></sub></form><legend id='c8UaN'></legend><bdo id='c8UaN'><pre id='c8UaN'><center id='c8UaN'></center></pre></bdo></b><th id='c8UaN'></th></span></q></dt></tr></i><div id='c8UaN'><tfoot id='c8UaN'></tfoot><dl id='c8UaN'><fieldset id='c8UaN'></fieldset></dl></div>

              <small id='c8UaN'></small><noframes id='c8UaN'>

              1. <legend id='c8UaN'><style id='c8UaN'><dir id='c8UaN'><q id='c8UaN'></q></dir></style></legend><tfoot id='c8UaN'></tfoot>

                本文介绍了如何从 Ajax 调用修改 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有这个代码:

                window.onload = function() {
                        document.cookie = 'foo=bar; expires=Sun, 01 Jan 2012 00:00:00 +0100; path=/';
                        var xhr = new XMLHttpRequest();
                        xhr.open("GET", "/showcookie.php",true);
                        xhr.setRequestHeader("Cookie", "foo=quux");
                
                        xhr.setRequestHeader("Foo", "Bar");
                        xhr.setRequestHeader("Foo", "Baz");
                
                        xhr.withCredentials = true;
                        var pre = document.getElementById('output');
                        xhr.onreadystatechange = function() {
                            if (4 == xhr.readyState) {
                                pre.innerHTML += xhr.responseText + "
                ";
                            }
                        };
                        xhr.send(null);
                    };
                

                还有这个/showcookie.php

                and this /showcookie.php

                <?php
                
                print_r($_COOKIE);
                
                ?>
                

                它总是显示

                Array
                (
                    [Host] => localhost
                    [User-Agent] => 
                    [Accept] => 
                    [Accept-Language] => pl,en-us;q=0.7,en;q=0.3
                    [Accept-Encoding] => gzip,deflate
                    [Accept-Charset] => ISO-8859-2,utf-8;q=0.7,*;q=0.7
                    [Keep-Alive] => 115
                    [Connection] => keep-alive
                    [foo] => Baz
                    [Referer] =>
                    [Cookie] => foo=bar
                )
                
                Array
                (
                    [foo] => bar
                )
                

                我在 Ubuntu 上使用 Firefox 3.6.13、Opera 11.00 和 Chromium 9.0.

                I'm using Firefox 3.6.13, Opera 11.00 and Chromium 9.0 on Ubuntu.

                有没有人有同样的问题,或者无法修改 Cookie 标头.

                Is anybody have the same problem or maybe it's impossible to modify Cookie header.

                推荐答案

                Cookie 标头是 XMLHttpRequest 中无法修改的几个标头之一.来自规范:

                The Cookie header is one of several which cannot be modified in an XMLHttpRequest. From the specification:

                终止[执行setRequestHeader方法]如果header是一个不区分大小写的匹配项之一以下标题:

                Terminate [execution of the setRequestHeader method] if header is a case-insensitive match for one of the following headers:

                • 接受字符集
                • 接受编码
                • 连接
                • 内容长度
                • Cookie
                • Cookie2
                • 内容传输编码
                • 日期
                • 期待
                • 主持人
                • 保持活跃
                • 推荐人
                • TE
                • 预告片
                • 传输编码
                • 升级
                • 用户代理
                • 通过

                ... 或者如果标题的开头是代理的不区分大小写匹配 - 或Sec- (包括当标题只是Proxy-或Sec-).

                … or if the start of header is a case-insensitive match for Proxy- or Sec- (including when header is just Proxy- or Sec-).

                以上标题由用户代理让它控制那些运输方面.这保证数据完整性在一定程度上.标题以 Sec- 开头的名称不是允许设置为允许新标头保证不会被铸造来自 XMLHttpRequest.

                The above headers are controlled by the user agent to let it control those aspects of transport. This guarantees data integrity to some extent. Header names starting with Sec- are not allowed to be set to allow new headers to be minted that are guaranteed not to come from XMLHttpRequest.

                这篇关于如何从 Ajax 调用修改 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:重用 XMLHttpRequest 对象还是创建一个新对象? 下一篇:Phantom JS 同步 AJAX 请求:NETWORK_ERR: XMLHttpRequest Exc

                相关文章

                最新文章

                <small id='wheYQ'></small><noframes id='wheYQ'>

              2. <tfoot id='wheYQ'></tfoot>
                <i id='wheYQ'><tr id='wheYQ'><dt id='wheYQ'><q id='wheYQ'><span id='wheYQ'><b id='wheYQ'><form id='wheYQ'><ins id='wheYQ'></ins><ul id='wheYQ'></ul><sub id='wheYQ'></sub></form><legend id='wheYQ'></legend><bdo id='wheYQ'><pre id='wheYQ'><center id='wheYQ'></center></pre></bdo></b><th id='wheYQ'></th></span></q></dt></tr></i><div id='wheYQ'><tfoot id='wheYQ'></tfoot><dl id='wheYQ'><fieldset id='wheYQ'></fieldset></dl></div>

                  1. <legend id='wheYQ'><style id='wheYQ'><dir id='wheYQ'><q id='wheYQ'></q></dir></style></legend>

                      <bdo id='wheYQ'></bdo><ul id='wheYQ'></ul>