php简单跨域
服务器apache(开启header扩展)
后台:
login.php:
<?php
//服务端返回JSON数据
$arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
$result=json_encode($arr);
//动态执行回调函数
header('Access-Control-Allow-Origin: http://test');
header( 'Access-Control-Allow-Credentials:true' );
echo $result;
前台:
ajax.html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>ajax跨域请求</title>
<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
//cros跨域:
$.ajax({
url: 'http://jack/login.php',
method: 'post',
crossDomain: true,
xhrFields:{
withCredentials: true
},
data: {
"username" : "rose",
"password" : "123456"
},
dataType: 'json',
success: function(data){
console.log(data);
}
});
});
</script>
</body>
</html>

浙公网安备 33010602011771号