# API 接口跨域
- application/api/behavior 创建 CORS.php
- 修改 application/tags.php 文件
- CORS.php
<?php | |
namespace app\api\behavior; | |
use think\Response; | |
class CORS | |
{ | |
public function appInit(&$params) | |
{ | |
$url = request()->Header("Origin"); | |
header('content-type:text/html;charset=utf-8'); | |
header('Access-Control-Allow-Credentials: true'); | |
//header('Access-Control-Allow-Origin: *'); | |
header('Access-Control-Allow-Origin: '.$url); | |
header("Access-Control-Allow-Headers: token,Origin, X-Requested-With, Content-Type, Accept"); | |
header('Access-Control-Allow-Methods: POST,GET,DELETE,PUT,PATCH'); | |
if (request()->isOptions()) { | |
exit(); | |
} | |
} | |
} |
- tags.php 部分
// 应用行为扩展定义文件 | |
return [ | |
// 应用初始化 | |
'app_init' => [ | |
'app\\api\\behavior\\CORS', | |
] | |
]; |