在前后端分离项目中,如通过thinkphp6开发api,涉及前端调用跨域问题,可通过自带的跨域中间件解决。
middleware.php中,设置

<?php
// 全局中间件定义文件
return [
    \think\middleware\AllowCrossDomain::class
];

等同于原生PHP中下面几行代码的效果。

<?php
    header('Content-Type: text/html;charset=utf-8');
    header('Access-Control-Allow-Origin:*'); 
    header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE');
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin');

相关文章:

  • 2022-01-14
  • 2022-12-23
  • 2021-07-17
  • 2021-10-15
  • 2021-08-05
  • 2021-06-17
  • 2021-07-15
  • 2022-12-23
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-03-02
相关资源
相似解决方案