thinkphp6.0前后端分离跨域处理

thinkphp6.0前后端分离跨域处理

  • 建立一个跨域中间键CrossDomain.php (tp6现在跨域中间键有点问题)
    thinkphp6.0前后端分离跨域处理
  • 中间键内代码CrossDomain.php
<?php
/**
 * Created by phpstorm
 * User: loveAKY
 * Date: 2021/1/18
 * Time: 17:06
 */

namespace app\middleware;

use think\Response;

/**
 * 跨域设置
 * Class CrossDomain
 * @package app\middleware
 */
class CrossDomain
{
    /**
     * 设置跨域
     * @param $request
     * @param \Closure $next
     *
     * @return mixed|void
     */
    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);
    }
}
  • 自动引用
    thinkphp6.0前后端分离跨域处理
    thinkphp6.0前后端分离跨域处理
上一篇:seafile部署安装


下一篇:Ajax跨域问题及解决方案