title: FastAdmin 個人記録 - behavior 解決クロスドメイン問題
date: 2021-09-10 17:33:29
tags:
- FastAdmin
- チュートリアル
category:
- FastAdmin
API インターフェースのクロスドメイン#
1. application/api/behavior に CORS.php を作成します。
2. 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',
]
];