关于跨域
这是之前加的
$origin = $_SERVER['HTTP_ORIGIN'] ?? '*'; header('Content-Type: text/html;charset=utf-8'); header('Access-Control-Allow-Origin:'. $origin); header('Access-Control-Allow-Methods:*'); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Headers: *');
但是请求之后报错
Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.
百度搜索了半天终于得到解决
在入口添加
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){ header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"); header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH'); exit; }
复杂请求时,会遇到options请求,但是thinkphp路由直接拒绝了options请求