ajax跨域訪問,可以使用jsonp方法或設定Access-Control-Allow-Origin實現,關於設定Access-Control-Allow-Origin實現跨域訪問可以參考之前我寫的文章《ajax 設定Access-Control- Allow-Origin實作跨網域存取》
先建立兩個測試網域
##a.fdipzone .com 作為客戶端網域名稱
b.fdipzone.com 作為服務端網域
測試程式碼
setcookie.php 用來設定服務端cookie
<?phpsetcookie('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');// 响应类型header('Access-Control-Allow-Methods:POST'); // 响应头设置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="//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>先執行 http://b.fdipzone.com/setcookie.php, 建立服務端cookie 。
然後執行http://a.fdipzone.com/test.html
輸出
{"success":true,"name":"fdipzone","cookie":""}取得cookie失敗。
客戶端 請求時將
withCredentials屬性設為true 讓可以指定某個請求應該要傳送憑證。如果伺服器接收有憑證的請求,會用下面的HTTP頭部來回應。
服務端 設定header
header("Access-Control-Allow-Credentials:true");允許請求帶有驗證訊息
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="//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}, // 发送凭据 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');// 响应类型header('Access-Control-Allow-Methods:POST'); // 响应头设置header('Access-Control-Allow-Headers:x-requested-with,content-type');// 是否允许请求带有验证信息header('Access-Control-Allow-Credentials:true'); header('content-type:application/json');echo json_encode($ret);?>按先前步驟執行,請求返回
{"success":true,"name":"fdipzone","cookie":"1484558863"}取得cookie成功
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.服務端header設定Access-Control-Allow-Credentials:true後,Access-Control-Allow-Origin不可以設為*,必須設定為域名,否則回傳錯誤。
XMLHttpRequest cannot load http://b.fdipzone.com/server.php. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://a.fdipzone.com' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.本篇文章說明了ajax跨網域存取cookie遺失的解決方法,更多相關內容請關注php中文網。 相關推薦:
關於mysql explain中key_len的計算方法講解
以上是關於ajax跨域訪問cookie丟失的解決方法的講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!