首页 > 其他 > 详细

tp5 跨域问题解决

时间:2021-07-13 20:30:15      阅读:14      评论:0      收藏:0      [点我收藏+]

在config.php 同级的tags.php中的【app_init】添加:

"app\\api\\behavior\\CORS"  例下图:

技术分享图片

 

然后在 app/behavior下新建文件:CORS.php,文件路径例下图:

技术分享图片

 

 

CORS.php文件内容:

<?php
namespace app\api\behavior;

use think\Response;

class CORS {

    public function appInit(){
        if (request()->isOptions()) {
            header(‘Access-Control-Allow-Origin:*‘);
            header(‘Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token‘);
            header(‘Access-Control-Allow-Credentials:true‘);
            header(‘Access-Control-Allow-Methods:GET,POST,OPTIONS,PUT,DELETE‘);
            header(‘Access-Control-Max-Age:1728000‘);
            header(‘Content-Type:text/plain charset=UTF-8‘);
            header(‘Content-Length: 0‘, true);
            header(‘status: 204‘);
            header(‘HTTP/1.0 204 No Content‘);
        }else{
            header(‘Access-Control-Allow-Origin:*‘);
            header(‘Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token‘);
            header(‘Access-Control-Allow-Credentials:true‘);
            header(‘Access-Control-Allow-Methods:GET,POST,OPTIONS‘);
        }
    }

}

 

over!!!

 

tp5 跨域问题解决

原文:https://www.cnblogs.com/j-jian/p/15007734.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!