//获取要跨域访问的请求源 $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; //定义允许跨域访问的请求源 $allow_origin = array('http://localhost:8080'); //如果请求源在允许列表中就设置允许访问 if (in_array($origin, $allow_origin)) { header('Access-Control-Allow-Origin:' . $origin); // *代表允许任何网址请求 } header('Content-Type: text/html;charset=utf-8'); // header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求 header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型 header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段 if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ header('Access-Control-Allow-Origin:*'); header('Access-Control-Allow-Methods:POST,GET,PUT,PATCH,OPTIONS,DELETE'); // 允许请求的类型 }
标签:Control,origin,跨域,Access,header,Allow,允许,终极,options From: https://www.cnblogs.com/jwyq/p/17276668.html