php、java设置多个Access-Control-Allow-Origin的方法
Access-Control-Allow-Origin只能设置一个值,要么是*,要么是单个域名,关键是还不支持泛域名*.bfw,wiki这种形式,怎么办,今天教大家一招,动态判断来路域名,然后再动态设置Access-Control-Allow-Origin
先看PHP怎么实现
<?php $origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; $allow_origin = array( 'http://www.bfw.wiki', 'http://img.bfw.wiki', ); if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (in_array($origin, $allow_origin)) { header("Access-Control-Allow-Origin:".$origin); header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Authorization"); header('Access-Control-Allow-Methods: GET, POST,OPTIONS,PATCH'); } exit(); } ?>
点击查看剩余70%
网友评论0