TP5接受Vue跨域請求
-
/**
-
* Created by PhpStorm.
-
* User: qianglong
-
* Date: 2018/1/15
-
* Time: 17:56
-
*/
-
namespace app\common\behavior;
-
use think\Exception;
-
use think\Response;
-
class CronRun
-
{
-
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;
-
}
-
}
-
}
1 在app頂層建立檔案common\behavior\CronRun.php 寫入以上程式碼
2 新增鉤子事件
在application \tags下寫入
-
<?php
-
// +----------------------------------------------------------------------
-
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
-
// +----------------------------------------------------------------------
-
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
-
// +----------------------------------------------------------------------
-
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
-
// +----------------------------------------------------------------------
-
// | Author: liu21st <[email protected]>
-
// +----------------------------------------------------------------------
-
// 應用行為擴充套件定義檔案
-
return [
-
// 應用初始化
-
'app_init' => [],
-
// 應用開始
-
'app_begin' => [
-
'app\\common\\behavior\\CronRun'
-
],
-
// 模組初始化
-
'module_init' => [],
-
// 操作開始執行
-
'action_begin' => [],
-
// 檢視內容過濾
-
'view_filter' => [],
-
// 日誌寫入
-
'log_write' => [],
-
// 應用結束
-
'app_end' => [
-
'app\\common\\behavior\\CronRun'
-
],
-
];