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)文章
在實(shí)戰(zhàn)中可能碰到的幾種ajax請(qǐng)求方法詳解
這篇文章主要給大家分享了在實(shí)戰(zhàn)中可能碰到的幾種ajax請(qǐng)求方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03
asp.net+Ajax 文本文件靜態(tài)分頁(yè)實(shí)現(xiàn)代碼
代碼還是很淺顯易懂的,呵呵因?yàn)槲乙膊粫?huì)寫高深的代碼。重要的我們都要?jiǎng)邮秩?shí)踐。菜鳥們多多努力哦。做完我這個(gè)例子級(jí)會(huì)收獲很多哦。2010-05-05
用ajax傳遞json到前臺(tái)中文出現(xiàn)問(wèn)號(hào)亂碼問(wèn)題的解決辦法
這篇文章主要介紹了用ajax傳遞json到前臺(tái)中文出現(xiàn)問(wèn)號(hào)亂碼問(wèn)題的解決辦法,需要的朋友參考下2017-01-01
Ajax實(shí)現(xiàn)省市區(qū)三級(jí)級(jí)聯(lián)(數(shù)據(jù)來(lái)自mysql數(shù)據(jù)庫(kù))
這篇文章主要為大家詳細(xì)介紹了Ajax實(shí)現(xiàn)省市區(qū)三級(jí)級(jí)聯(lián),數(shù)據(jù)來(lái)自mysql數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Jquery $.ajax函數(shù)外的一段代碼的執(zhí)行順序
調(diào)用了JQuery的異步函數(shù)$.ajax ,然后在$.ajax函數(shù)外之后又有一段Jquery 代碼。每次都是在$.ajax之后的代碼先執(zhí)行2014-06-06
[asp]天楓AJAX百度音樂(lè)即時(shí)聽附下載
[asp]天楓AJAX百度音樂(lè)即時(shí)聽附下載...2007-09-09

