PHP TP框架api跨域问题

在公共控制器中添加如下代码:

function _initialize()
{
   header("Access-Control-Allow-Origin: *");
   header("Access-Control-Allow-Methods:POST,GET");
   header("Access-Control-Allow-Headers:x-requested-with,content-type");
   header("Content-type:text/json;charset=utf-8");
}

在入口文件添加:

if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
    header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');
    return \think\Response::create()->send();
}

完成!

发布了19 篇原创文章 · 获赞 6 · 访问量 2551

猜你喜欢

转载自blog.csdn.net/qq_42922085/article/details/95458450