php 跨域处理

header('Content-type: text/html; charset=utf-8');
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
//header('Access-Control-Allow-Origin: ' . $origin);
header('Access-Control-Allow-Origin: *'); // 支持跨域,允许所有访问
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Credentials: true');

/**
* 浏览器第一次在处理复杂请求的时候会先发起OPTIONS请求。路由在处理请求的时候会导致PUT请求失败。
* 在检测到option请求的时候就停止继续执行
*/
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
    exit;
}

 

posted @ 2021-07-19 10:31  林间有风-邓  阅读(93)  评论(0编辑  收藏  举报