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.  
  9. use think\Exception;

  10. use think\Response;

  11.  
  12. class CronRun

  13. {

  14. public function run(&$dispatch){

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

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

  17. $headers = [

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

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

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

  21. ];

  22. if($dispatch instanceof Response) {

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

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

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

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

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

  28. }

  29. }

  30. }

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. // 应用行为扩展定义文件

  13. return [

  14. // 应用初始化

  15. 'app_init' => [],

  16. // 应用开始

  17. 'app_begin' => [

  18. 'app\\common\\behavior\\CronRun'

  19. ],

  20. // 模块初始化

  21. 'module_init' => [],

  22. // 操作开始执行

  23. 'action_begin' => [],

  24. // 视图内容过滤

  25. 'view_filter' => [],

  26. // 日志写入

  27. 'log_write' => [],

  28. // 应用结束

  29. 'app_end' => [

  30. 'app\\common\\behavior\\CronRun'

  31. ],

  32. ];

猜你喜欢

转载自blog.csdn.net/qq_42030417/article/details/82700970