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跨域訪問,cookie丟失
首先創(chuàng)建兩個測試域名
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="http://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.解決方法
客戶端
請求時將withCredentials屬性設(shè)置為true
使可以指定某個請求應(yīng)該發(fā)送憑據(jù)。如果服務(wù)器接收帶憑據(jù)的請求,會用下面的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="http://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,請求時會返回錯誤。
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è)置為一個域名,否則回返回錯誤。
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丟失問題的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能
本文主要介紹了ajax實(shí)現(xiàn)數(shù)據(jù)刪除、查看詳情功能,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03AJAX原理以及axios、fetch區(qū)別實(shí)例詳解
前端是個發(fā)展迅速的領(lǐng)域,前端請求自然也發(fā)展迅速,從原生的XHR到j(luò)query ajax,再到現(xiàn)在的axios和fetch,下面這篇文章主要給大家介紹了關(guān)于AJAX原理以及axios、fetch區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-04-04JQuery中Ajax的Post提交在IE下中文亂碼的解決方法
在JQuery的Ajax POST請求中,進(jìn)行請求,其中的中文在后臺,顯示為亂碼,在FF/Chrome中,可以正常傳遞中文,但是在IE下,則存在問題2014-05-05ajax 數(shù)據(jù)庫中隨機(jī)讀取5條數(shù)據(jù)動態(tài)在頁面中刷新
以下是我在此編寫一個程序時的時刻所遇到的問題。因?yàn)橐婚_始經(jīng)理給我分配了要我寫一個在頁面上動態(tài)的顯示5條數(shù)據(jù)。2009-06-06jQuery實(shí)現(xiàn)AJAX定時刷新局部頁面實(shí)例
本篇文章通過兩種方法實(shí)例講解ajax定時刷新局部頁面,當(dāng)然方法有很多種,也可以不使用ajax來刷新頁面,可以使用jquery中的append來給指定內(nèi)容加?xùn)|西,但是都不太實(shí)用,最實(shí)用的方法還是ajax加載數(shù)據(jù)了。2015-09-09利用Ajax實(shí)現(xiàn)在腳本里傳值實(shí)例介紹
Ajax實(shí)現(xiàn)在腳本里傳值可以解決實(shí)際上的一些問題,本文實(shí)現(xiàn)了一下,感興趣的朋友可以參考下,希望可以幫助到你2013-04-04滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
滾動下拉到頁面底部加載數(shù)據(jù)是很多瀑布流網(wǎng)站的做法,那來看看配合jsonp是如何實(shí)現(xiàn)的吧,小菜總結(jié)記錄之用特在此與大家一起分享,感興趣的朋友可以參考下哈2013-05-05