TP5.0 跨域问题

浏览:2005 发布日期:2020/04/27 分类:ThinkPHP5专区 关键字: tp5.0,跨域,Nginx,ajax
网上所有的方法都试过了,还是无法解决
第一种方法,在入口文件:public/index.phpheader("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS,DELETE");
header("Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept");
无效~


第二种方法:挂钩子
在application/下新建一个CORS.phpnamespace app\sendApi;
use think\Response;

class CORS {
public function run(&$dispatch){

header("Access-Control-Allow-Origin:*");

$host_name = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : "*";

$headers = [

"Access-Control-Allow-Origin" => $host_name,

"Access-Control-Allow-Credentials" => 'true',

"Access-Control-Allow-Headers" => "x-token,x-uid,x-token-check,x-requested-with,content-type,Host"

];

if($dispatch instanceof Response) {

$dispatch->header($headers);

} else if($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {

$dispatch['type'] = 'response';

$response = new Response('', 200, $headers);

$dispatch['response'] = $response;

}

}
}
同时在tag.php中的app_init,app_begin,app_end中都添加了路径,确定路径是正确的,但是仍然显示跨域


最佳答案
评论( 相关
后面还有条评论,点击查看>>