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