amber

amber

FastAdmin 個人記錄 - behavior解決跨域問題


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',
    ]
];
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。