1. 程式人生 > >TP5接受Vue跨域請求

TP5接受Vue跨域請求

  1. /**

  2. * Created by PhpStorm.

  3. * User: qianglong

  4. * Date: 2018/1/15

  5. * Time: 17:56

  6. */

  7. namespace app\common\behavior;

  8. use think\Exception;

  9. use think\Response;

  10. class CronRun

  11. {

  12. public function run(&$dispatch){

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

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

  15. $headers = [

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

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

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

  19. ];

  20. if($dispatch instanceof Response) {

  21. $dispatch->header($headers);

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

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

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

  25. $dispatch['response'] = $response;

  26. }

  27. }

  28. }

1 在app頂層建立檔案common\behavior\CronRun.php 寫入以上程式碼

2 新增鉤子事件

在application \tags下寫入

  1. <?php

  2. // +----------------------------------------------------------------------

  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]

  4. // +----------------------------------------------------------------------

  5. // | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.

  6. // +----------------------------------------------------------------------

  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

  8. // +----------------------------------------------------------------------

  9. // | Author: liu21st <[email protected]>

  10. // +----------------------------------------------------------------------

  11. // 應用行為擴充套件定義檔案

  12. return [

  13. // 應用初始化

  14. 'app_init' => [],

  15. // 應用開始

  16. 'app_begin' => [

  17. 'app\\common\\behavior\\CronRun'

  18. ],

  19. // 模組初始化

  20. 'module_init' => [],

  21. // 操作開始執行

  22. 'action_begin' => [],

  23. // 檢視內容過濾

  24. 'view_filter' => [],

  25. // 日誌寫入

  26. 'log_write' => [],

  27. // 應用結束

  28. 'app_end' => [

  29. 'app\\common\\behavior\\CronRun'

  30. ],

  31. ];