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,关于php:tp6-通过全局中间件-解决跨域问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信token失效时间
- 下一篇: Linux中断(interrupt)子系