共计 674 个字符,预计需要花费 2 分钟才能阅读完成。
tp6 官网有提供跨域决绝办法,当我间接应用无奈用。(可能我用的姿态不对)。
定义中间件
<?php
declare (strict_types = 1);
namespace app\middleware;
use think\Response;
/**
* 全局跨域申请解决
* Class CrossDomain
* @package app\middleware
*/
class CrossDomain
{public function handle($request, \Closure $next)
{header('Access-Control-Allow-Origin: *');
header('Access-Control-Max-Age: 1800');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
header('Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, Token');
if (strtoupper($request->method()) == "OPTIONS") {return Response::create()->send();}
return $next($request);
}
}
在 middleware.php 中退出咱们定义的中间件
而后跨域就好使了!
正文完