论坛风格切换
  • 221阅读
  • 0回复

[功能优化与改进]解决thinkphp应对options请求的时候的跨域问题 [复制链接]

上一主题 下一主题
离线hduxg
 

发帖
12
金币
56
威望
11
只看楼主 倒序阅读 使用道具 楼主  发表于: 2020-11-23
关键词: Array
复杂请求时,会遇到options请求,但是thinkphp路由直接拒绝了options请求,把头修改加到控制器里面是没用的。设置路由暂时还不会。不过可以直接加到入口文件


  1. if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
  2.         header("Access-Control-Allow-Origin: *");
  3.         header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
  4.         header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
  5.     exit;
  6. }



tp3.2和tp5的区别

tp3.2可以在文件头部加
  1. if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
  2.     D('Aaaa')->data(array('space'=>'applet', 'module'=>'wxtoken', 'flag'=>'$_REQUEST', 'content'=>pw_var_export($_REQUEST), 'create_time'=>date("Y-m-d H:i:s")))->add();
  3.     header("Access-Control-Allow-Origin: *");
  4.     header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, CAPTCHA");
  5.     header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
  6.     file_put_contents('option.txt',json_encode($_REQUEST));
  7.     exit;
  8. }
  9. header('Access-Control-Allow-Origin:*');
  10. header('Access-Control-Allow-Methods:*'); //响应类型
  11. header('Access-Control-Allow-Headers:content-type,token,id'); //响应头设置
  12. header("Access-Control-Request-Headers: Origin, X-Requested-With, content-Type, Accept, Authorization, CAPTCHA");

但是tp5这么加无效,
  1. if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
  2.     header("Access-Control-Allow-Origin: *");
  3.     header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, CAPTCHA");
  4.     header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
  5.     exit;
  6. }

必须加在入口文件里面。


快速回复
限100 字节
批量上传需要先选择文件,再选择上传
 
提到某人:
选择好友
上一个 下一个