php解决跨域问题6,关于php:tp6-通过全局中间件-解决跨域问题
tp6官網(wǎng)有提供跨域決絕辦法,當(dāng)我間接應(yīng)用無奈用。(可能我用的姿態(tài)不對(duì))。
定義中間件
declare (strict_types = 1);
namespace app\middleware;
use think\Response;
/**
* 全局跨域申請(qǐng)解決
* 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中退出咱們定義的中間件
【騰訊云】云產(chǎn)品限時(shí)秒殺,爆款1核2G云服務(wù)器,首年99元
而后跨域就好使了!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的php解决跨域问题6,关于php:tp6-通过全局中间件-解决跨域问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信token失效时间
- 下一篇: 图片加尺寸php代码,php获取图片尺寸