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();
}

完成!

你可能感兴趣的:(PHP)