当前后端分离,跨域时需要使用到中间件
在文件middleware.php 文件中需要创建 代码
<?php
// 全局中间件定义文件
return [
// 全局请求缓存
// \think\middleware\CheckRequestCache::class,
// 多语言加载
// \think\middleware\LoadLangPack::class,
// Session初始化
// \think\middleware\SessionInit::class
\app\middleware\CrossDomain::class
];
在文件夹下面创建CrossDomain.php 文件,代码为
<?php
/**
* Created by phpstorm
* User: loveAKY
* Date: 2021/1/18
* Time: 17:06
*/
namespace app\middleware;
use think\Response;
/**
* 跨域设置
*/
class CrossDomain
{
/**
* [handle 设置跨域]
* @Author 1076986776@qq.com
* @DateTime 2022-02-03
* @version [version]
* @param [type] $request [description]
* @param \Closure $next [description]
* @return