Blogger Information
Blog 64
fans 6
comment 2
visits 82977
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
tp6解决跨域问题
王娇
Original
4317 people have browsed it
  • 终端中输入命令 新建一个Cross中间件
    php think make:middleware Cross
  1. * 设置跨域
  2. * @param $request
  3. * @param \Closure $next
  4. * @return mixed|void
  5. */
  6. public function handle($request, \Closure $next)
  7. {
  8. header('Access-Control-Allow-Origin: *');
  9. header('Access-Control-Max-Age: 1800');
  10. header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE');
  11. 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');
  12. if (strtoupper($request->method()) == "OPTIONS") {
  13. return Response::create()->send();
  14. }
  15. return $next($request);
  16. }
  • 在app下的middleware.php中使用中间件
  1. <?php
  2. // 全局中间件定义文件
  3. return [
  4. // 全局请求缓存
  5. // \think\middleware\CheckRequestCache::class,
  6. // 多语言加载
  7. // \think\middleware\LoadLangPack::class,
  8. // Session初始化
  9. // \think\middleware\SessionInit::class
  10. //解决跨域问题 自定义Cross中间件
  11. \app\middleware\Cross::class
  12. ];
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post