## 如何跨域 跨域只需在前置中间件增加跨域的代码,如下: ~~~ <?php namespace apps\httpd\middleware; use mix\facades\Request; use mix\facades\Response; /** * 前置中间件 * @author 刘健 <coder.liu@qq.com> */ class BeforeMiddleware { public function handle($callable, \Closure $next) { // 添加中间件执行代码 list($controller, $action) = $callable; // 跨域设置 $origin = Request::header('origin'); $allowOrigin = [ 'http://www.test.com', 'http://www.test1.com', ]; if (in_array($origin, $allowOrigin)) { Response::setHeader('Access-Control-Allow-Origin', $origin); Response::setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization'); Response::setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS, PATCH'); } if (Request::isOptions()) { return ''; } // 执行下一个中间件 return $next(); } } ~~~ - Access-Control-Allow-Origin:允许跨域的域名 - Access-Control-Allow-Headers:允许跨域的请求头 - Access-Control-Allow-Methods:允许跨域的请求方法