1 .  application目录下新建http/middleware/CrossDomain.php

<?php
/**
* 请求处理中间件
* Author: weiyongqiang<hayixia606@163.com>
* Date: 2018/9/27
* Time: 11:02
*/
namespace app\http\middleware;
use think\Response;
class CrossDomain
{
public function handle($request, \Closure $next)
{
header(‘Access-Control-Allow-Origin: http://localhost:8080’);
header(‘Access-Control-Allow-Credentials: true’);
header(‘Access-Control-Allow-Headers: Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With’);
header(‘Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE’);
header(‘Access-Control-Max-Age: 1728000’);
if (strtoupper($request->method()) == “OPTIONS”) {
return Response::create()->send();
}
return $next($request);
}
}

2.  需要的模块下middleware.php(与controller目录 同目录):

<?php
/**
* 中间件注册
* Author: weiyongqiang<hayixia606@163.com>
* Date: 2018/9/27
* Time: 11:49
*/
return [
\app\http\middleware\CrossDomain::class
];