zoukankan      html  css  js  c++  java
  • tp5之允许跨域请求

    一、在app顶层创建文件commonehaviorCronRun.php 写入以下代码

     
    <?php

    namespace appcommonehavior; use thinkException; use thinkResponse; 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; } } }

    二、 添加钩子事件

    在application ags下写入

    <?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 <liu21st@gmail.com>
    // +----------------------------------------------------------------------
     
    // 应用行为扩展定义文件
    return [
        // 应用初始化
        'app_init'     => [],
        // 应用开始
        'app_begin'    => [
            'app\common\behavior\CronRun'
        ],
        // 模块初始化
        'module_init'  => [],
        // 操作开始执行
        'action_begin' => [],
        // 视图内容过滤
        'view_filter'  => [],
        // 日志写入
        'log_write'    => [],
        // 应用结束
        'app_end'      => [
            'app\common\behavior\CronRun'
        ],
    ];

      

    接下来就可以跨域请求了

    参考博客:https://blog.csdn.net/wyk9916/article/details/82315700

    let the world have no hard-to-write code ^-^
  • 相关阅读:
    工厂模式之简单工厂模式
    JavaMail(二):利用JavaMail发送复杂邮件
    JavaMail(一):利用JavaMail发送简单邮件
    浅谈Java中静态代码块和非静态代码块
    MySQL中常用转换函数介绍
    jquery easyui datagrid 分页 详解
    tomcat出现的PermGen Space问题(bat,或者eclipse启动。)
    把Blob 转化为字符串查询。
    日期相关---SimpleDateFormat的setLenient(true/false)-----自动计算日期
    处理同一个字段,多种日期格式
  • 原文地址:https://www.cnblogs.com/ovim/p/11308672.html
Copyright © 2011-2022 走看看