<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

        Ajax跨域訪問Cookie丟失問題的解決方法

        來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:51:43
        文檔

        Ajax跨域訪問Cookie丟失問題的解決方法

        Ajax跨域訪問Cookie丟失問題的解決方法:ajax跨域訪問,可以使用jsonp方法或設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn),關(guān)于設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問可以參考之前我寫的文章《ajax 設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問》 1.ajax跨域訪問,cook
        推薦度:
        導(dǎo)讀Ajax跨域訪問Cookie丟失問題的解決方法:ajax跨域訪問,可以使用jsonp方法或設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn),關(guān)于設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問可以參考之前我寫的文章《ajax 設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問》 1.ajax跨域訪問,cook

        ajax跨域訪問,可以使用jsonp方法或設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn),關(guān)于設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問可以參考之前我寫的文章《ajax 設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問》

        1.ajax跨域訪問,cookie丟失

        首先創(chuàng)建兩個(gè)測試域名

        a.fdipzone.com 作為客戶端域名

        b.fdipzone.com 作為服務(wù)端域名

        測試代碼

        setcookie.PHP 用于設(shè)置服務(wù)端cookie

        <?php
        setcookie('data', time(), time()+3600);
        ?>

        server.php 用于被客戶端請求

        <?php
        $name = isset($_POST['name'])? $_POST['name'] : '';
        $ret = array(
         'success' => true,
         'name' => $name,
         'cookie' => isset($_COOKIE['data'])? $_COOKIE['data'] : ''
        );
        // 指定允許其他域名訪問
        header('Access-Control-Allow-Origin:http://a.fdipzone.com');
        // 響應(yīng)類型
        header('Access-Control-Allow-Methods:POST'); 
        // 響應(yīng)頭設(shè)置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        header('content-type:application/json');
        echo json_encode($ret);
        ?>

        test.html 客戶端請求頁面

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
         <head>
         <meta http-equiv="content-type" content="text/html;charset=utf-8">
         <script src="https://www.gxlcms.com//code.jquery.com/jquery-1.11.0.min.js"></script>
         <title> ajax 跨域訪問cookie丟失的解決方法 </title>
         </head>
         <body>
         <script type="text/javascript">
         $(function(){
         $.ajax({
         url: 'http://b.fdipzone.com/server.php', // 跨域
         dataType: 'json',
         type: 'post',
         data: {'name':'fdipzone'},
         success:function(ret){
         if(ret['success']==true){
         alert('cookie:' + ret['cookie']);
         }
         }
         });
         })
         </script>
         </body>
        </html>

        首先先執(zhí)行http://b.fdipzone.com/setcookie.php, 創(chuàng)建服務(wù)端cookie。

        然后執(zhí)行http://a.fdipzone.com/test.html

        輸出

        {"success":true,"name":"fdipzone","cookie":""}

        獲取cookie失敗。

        2.解決方法

        客戶端

        請求時(shí)將withCredentials屬性設(shè)置為true

        使可以指定某個(gè)請求應(yīng)該發(fā)送憑據(jù)。如果服務(wù)器接收帶憑據(jù)的請求,會(huì)用下面的HTTP頭部來響應(yīng)。

        服務(wù)端

        設(shè)置header

        header("Access-Control-Allow-Credentials:true");

        允許請求帶有驗(yàn)證信息

        test.html 修改如下

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
         <head>
         <meta http-equiv="content-type" content="text/html;charset=utf-8">
         <script src="https://www.gxlcms.com//code.jquery.com/jquery-1.11.0.min.js"></script>
         <title> ajax 跨域訪問cookie丟失的解決方法 </title>
         </head>
         <body>
         <script type="text/javascript">
         $(function(){
         $.ajax({
         url: 'http://b.fdipzone.com/server.php', // 跨域
         xhrFields:{withCredentials: true}, // 發(fā)送憑據(jù)
         dataType: 'json',
         type: 'post',
         data: {'name':'fdipzone'},
         success:function(ret){
         if(ret['success']==true){
         alert('cookie:' + ret['cookie']);
         }
         }
         });
         })
         </script>
         </body>
        </html>

        server.php 修改如下

        <?php
        $name = isset($_POST['name'])? $_POST['name'] : '';
        $ret = array(
         'success' => true,
         'name' => $name,
         'cookie' => isset($_COOKIE['data'])? $_COOKIE['data'] : ''
        );
        // 指定允許其他域名訪問
        header('Access-Control-Allow-Origin:http://a.fdipzone.com');
        // 響應(yīng)類型
        header('Access-Control-Allow-Methods:POST'); 
        // 響應(yīng)頭設(shè)置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        // 是否允許請求帶有驗(yàn)證信息
        header('Access-Control-Allow-Credentials:true');
        header('content-type:application/json');
        echo json_encode($ret);
        ?>

        按之前步驟執(zhí)行,請求返回

        {"success":true,"name":"fdipzone","cookie":"1484558863"}

        獲取cookie成功

        3.注意事項(xiàng)

        1.如果客戶端設(shè)置了withCredentials屬性設(shè)置為true,而服務(wù)端沒有設(shè)置Access-Control-Allow-Credentials:true,請求時(shí)會(huì)返回錯(cuò)誤。

        XMLHttpRequest cannot load http://b.fdipzone.com/server.php. Credentials flag is 'true', but the 'Access-Control-Allow-Credentials' header is ''. It must be 'true' to allow credentials. Origin 'http://a.fdipzone.com' is therefore not allowed access.

        2.服務(wù)端header設(shè)置Access-Control-Allow-Credentials:true后,Access-Control-Allow-Origin不可以設(shè)為*,必須設(shè)置為一個(gè)域名,否則回返回錯(cuò)誤。

        XMLHttpRequest cannot load http://b.fdipzone.com/server.php. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' heade

        下面看下Ajax跨域請求COOKIE無法帶上的解決辦法

        原生ajax請求方式:

        var xhr = new XMLHttpRequest(); 
        xhr.open("POST", "http://xxxx.com/demo/b/index.php", true); 
        xhr.withCredentials = true; //支持跨域發(fā)送cookies
        xhr.send();

        jquery的ajax的post方法請求:

         $.ajax({
         type: "POST",
         url: "http://xxx.com/api/test",
         dataType: 'jsonp',
         xhrFields: {
         withCredentials: true
         },
         crossDomain: true,
         success:function(){
         },
         error:function(){
         }
        })

        服務(wù)器端設(shè)置:

        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Origin: http://www.xxx.com");

        以上所述是小編給大家介紹的Ajax跨域訪問Cookie丟失問題的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Ajax跨域訪問Cookie丟失問題的解決方法

        Ajax跨域訪問Cookie丟失問題的解決方法:ajax跨域訪問,可以使用jsonp方法或設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn),關(guān)于設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問可以參考之前我寫的文章《ajax 設(shè)置Access-Control-Allow-Origin實(shí)現(xiàn)跨域訪問》 1.ajax跨域訪問,cook
        推薦度:
        標(biāo)簽: 方法 ajax ajax跨域
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 免费精品国产自产拍在| 精品熟女少妇av免费久久| 亚洲人成欧美中文字幕| 亚洲精品理论电影在线观看 | 成人永久免费高清| 亚洲成A人片在线观看中文| 国产亚洲人成无码网在线观看| 亚洲精品视频在线| 亚洲变态另类一区二区三区| 成人激情免费视频| 亚洲永久无码3D动漫一区| 国产日本亚洲一区二区三区| 男女污污污超污视频免费在线看| 免费人成毛片动漫在线播放| 成人午夜18免费看| 老司机午夜在线视频免费| 久久不见久久见免费影院| 亚洲精品白浆高清久久久久久| 国产免费网站看v片在线| 国产午夜影视大全免费观看| 亚洲人成影院在线| 欧美大尺寸SUV免费| 粉色视频成年免费人15次 | 女性无套免费网站在线看| 亚洲成人中文字幕| 一级成人生活片免费看| 免费国产黄线在线观看| 免费大片av手机看片高清| 亚洲国产综合精品中文第一区| www.黄色免费网站| 亚洲最大视频网站| 久久综合九色综合97免费下载| 亚洲AV无码专区日韩| 午夜老司机永久免费看片| 亚洲av无码成人黄网站在线观看 | 亚洲人成色77777在线观看大| 亚洲欧美日韩自偷自拍| 亚洲自偷自偷在线制服| 一级女性全黄久久生活片免费| 国产v片免费播放| 免费无遮挡无码永久视频|