php+ajax跨域请求解决方法

在请求的php文件头部添加,即可解决php+ajax跨域请求问题

header('Access-Control-Allow-Origin:*');

header('Access-Control-Allow-Methods:POST,GET');

header('Access-Control-Allow-Headers:x-requested-with,content-type');

 

案例:

<?php 

header('Access-Control-Allow-Origin:*');

header('Access-Control-Allow-Methods:POST');

header('Access-Control-Allow-Headers:x-requested-with,content-type');

 

$arr=array();

$arr=json_encode_ex($arr);

$arr=str_replace("\\/", "/", $arr);

echo $arr;
exit();

function json_encode_ex($value)
{
if (version_compare(PHP_VERSION,'5.4.0','<'))
{
$str = json_encode($value);
$str = preg_replace_callback(
"#\\\u([0-9a-f]{4})#i",
function($matchs)
{
return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
},
$str
);
return $str;
}
else
{
return json_encode($value, JSON_UNESCAPED_UNICODE);
}
}

?>

posted @ 2018-11-15 16:32  TE网站建设工作室  阅读(421)  评论(0)    收藏  举报