tp5.0跨域问题

在本地
在这里插入图片描述在这里插入图片描述跨域
使用第一种:

<?php
/**
 * Created by PhpStorm.
 * User: M_jh
 * Date: 2019/1/18
 * Time: 15:04
 */

namespace app\api\behavior;

use think\Response;
class CORS
{
    public function appInit(&$params)
    {

        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: token, Origin, X-Requested-With, Content-Type, Accept, Authorization");
        header('Access-Control-Allow-Methods: POST,GET,PUT,DELETE');

        if (request()->isOptions()){
            exit();
        }

    }
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'app_init'     => [
        'app\\api\\behavior\\CORS'
    ],

在这里插入图片描述

使用方法二:

<?php
/**
 * Created by PhpStorm.
 * User: M_jh
 * Date: 2019/1/18
 * Time: 21:16
 */
namespace service;
class ToolsService
{
    public static function corsOptionsHandler()
    {
        if (request()->isOptions())
        {
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
            header('Access-Control-Allow-Credentials:true');
            header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
            header('Access-Control-Max-Age:1728000');
            header('Content-Type:text/plain charset=UTF-8');
            header('Content-Length: 0', true);
            header('status: 204');
            header('HTTP/1.0 204 No Content');
        }else{
            header('Access-Control-Allow-Origin:*');
            header('Access-Control-Allow-Headers:Accept,Referer,Host,Keep-Alive,User-Agent,X-Requested-With,Cache-Control,Content-Type,Cookie,token');
            header('Access-Control-Allow-Credentials:true');
            header('Access-Control-Allow-Methods:GET,POST,OPTIONS');
        }
    }

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
/**
 * Created by PhpStorm.
 * User: M_jh
 * Date: 2019/1/18
 * Time: 21:19
 */

namespace app\api\controller\v1;


use think\Controller;
use ervice\ToolsService;

class BaseController extends Controller
{
    /**
     * 基础接口
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        // CORS 跨域 Options 检测响应
        ToolsService::corsOptionsHandler();
    }
}

在这里插入图片描述问题出在跨域,但是借助网上这两种方法都不成功,都是同样的返回提示。现在问题不是这两种方式,搞不清楚问题出在哪儿了?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值