php解决跨域问题6,关于php:tp6-通过全局中间件-解决跨域问题

tp6官网有提供跨域决绝办法,当我间接应用无奈用。(可能我用的姿态不对)。

定义中间件

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中退出咱们定义的中间件

【腾讯云】云产品限时秒杀,爆款1核2G云服务器,首年99元

而后跨域就好使了!

你可能感兴趣的:(php解决跨域问题6)