html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here1</title> <script type="text/javascript" src="jq.js"></script> </head> <body> <?php echo "s";?> <script type="text/javascript"> jQuery(document).ready(function(){ $.ajax({ type: "get", async: false, url: "http://www.services.com/dadianlogin/login?username=1111&password=11111", dataType: "jsonp", jsonp: "callback",//傳遞給請求處理程序或頁面的,用以獲得jsonp回調函數名的參數名(一般默認為:callback) jsonpCallback:"returnjs",//自定義的jsonp回調函數名稱,默認為jQuery自動生成的隨機函數名,也可以寫"?",jQuery會自動為你處理數據 crossDomain:true, success: function(json){ alert(json.code); }, error: function(){ alert('fail'); } }); }); </script> </body> </html>
接收后台: <?php class dadianlogin{ public function login(){ $username=$_GET['username']; $password=$_GET['password']; $ticke=111111; header("Location:http://www.return.com/dandian/setcookie?ticke=$ticke"); } }
跨域后台: <?php class dandian{ //return 域下會設置一個cookie public function setcookie(){ $ticke=$_GET['ticke']; setcookie("ticke",$ticke); $code='yes'; echo 'returnjs({"code":"'.$code.'"})'; die; } }
應用場景:send傳輸數據進行登錄 services負責驗證用戶正確與否並生成安全票據 return 獲得票據,成功登錄