搜索 |
;(function(){
var url="http://********/Home/Check/checksession",
tourl="http://********/Home/Index/index";
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data:"",
xhrFields: {
withCredentials: true
},
error:function(){
console.log("error")
},
success:function(ret){
if(!ret.status){
window.top.location.href=tourl;
}
}
})
})();
php
<?php
namespace Home\Controller;
use Think\Controller;
error_reporting ( 0 );
header("Content-type: text/html; charset=utf-8");
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Origin:http://********:8055');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
class CheckController extends Controller {
public function checksession(){
if (session("cgptgx.id")) {
$data['msg']="success";
$data["status"]=1;
$this->ajaxReturn($data);
}else{
$data['msg']=session("cgptgx.id");
$data["status"]=0;
$this->ajaxReturn($data);
}
}
}
?>
?
//允许多个域名请求
<?php
session_start();
error_reporting ( 0 );
header("Content-type: text/html; charset=utf-8");
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array( 'http://********:3021','http://********:3021');
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin.'');
}
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
if($_SESSION['username']){
$data['status']=1;
$data['msg']=$_SESSION['username'];
echo json_encode($data);
}else{
$data['status']=0;
$data['msg']=$_SESSION['username'];
echo json_encode($data);
}
?>
方法2:利用jsonp
//js
$(function(){
$.ajax({
url:"http://********:8063/test.php",
type: 'get',
dataType: 'jsonp',
data:{name:"ttt",pwd:"ttt."},
error:function(){
console.log("error")
},
success:function(ret){
console.log(ret.name);
}
})
})
//php
header("Content-type:tex/html;charset=utf8");
session_start();
$_SESSION['user']="test";
$data['name']=$_GET['name'];
$data['pwd']=$_GET['pwd'];
$data['user']=$_SESSION['user'];
echo $_GET['callback']."(".json_encode($data).")";