第一种方法,在入口文件:public/index.php
header("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.php
namespace 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中都添加了路径,确定路径是正确的,但是仍然显示跨域最佳答案