最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Ajax跨域訪問(wèn)Cookie丟失問(wèn)題的解決方法

 更新時(shí)間:2017年02月03日 10:25:47   投稿:mrr  
這篇文章主要介紹了Ajax跨域訪問(wèn)Cookie丟失問(wèn)題的解決方法,需要的朋友可以參考下

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

1.ajax跨域訪問(wèn),cookie丟失

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

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

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

測(cè)試代碼

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

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

server.php 用于被客戶端請(qǐng)求

<?php
$name = isset($_POST['name'])? $_POST['name'] : '';
$ret = array(
 'success' => true,
 'name' => $name,
 'cookie' => isset($_COOKIE['data'])? $_COOKIE['data'] : ''
);
// 指定允許其他域名訪問(wèn)
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 客戶端請(qǐng)求頁(yè)面

<!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="http://code.jquery.com/jquery-1.11.0.min.js"></script>
 <title> ajax 跨域訪問(wèn)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.解決方法

客戶端

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

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

服務(wù)端

設(shè)置header

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

允許請(qǐng)求帶有驗(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="http://code.jquery.com/jquery-1.11.0.min.js"></script>
 <title> ajax 跨域訪問(wèn)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'] : ''
);
// 指定允許其他域名訪問(wèn)
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');
// 是否允許請(qǐng)求帶有驗(yàn)證信息
header('Access-Control-Allow-Credentials:true');
header('content-type:application/json');
echo json_encode($ret);
?>

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

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

獲取cookie成功

3.注意事項(xiàng)

1.如果客戶端設(shè)置了withCredentials屬性設(shè)置為true,而服務(wù)端沒有設(shè)置Access-Control-Allow-Credentials:true,請(qǐng)求時(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跨域請(qǐng)求COOKIE無(wú)法帶上的解決辦法

原生ajax請(qǐng)求方式:

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方法請(qǐng)求:

 $.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跨域訪問(wèn)Cookie丟失問(wèn)題的解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

连南| 临武县| 上蔡县| 来安县| 鄂伦春自治旗| 庄浪县| 天长市| 高唐县| 天镇县| 青阳县| 苏尼特右旗| 峨山| 新竹市| 玉山县| 通江县| 清水县| 库尔勒市| 大名县| 蓬溪县| 沈阳市| 桃园县| 武冈市| 基隆市| 雷山县| 鹤峰县| 米脂县| 长沙市| 文昌市| 镇原县| 五台县| 花垣县| 交口县| 营口市| 长葛市| 保康县| 张家口市| 清丰县| 赫章县| 商都县| 涞源县| 屏边|