vue+axios每次都重复请求两次解决

使用Thinkphp6 开启全局跨域

修改文件 app/middleware.php 增加 \think\middleware\AllowCrossDomain::class

<?php
// 全局中间件定义文件
return [
    // 全局请求缓存
    // \think\middleware\CheckRequestCache::class,
    // 多语言加载
    // \think\middleware\LoadLangPack::class,
    // Session初始化
    // \think\middleware\SessionInit::class
    // 开启跨域
    \think\middleware\AllowCrossDomain::class
];

第一次的 OPTIONS请求处理

修改文件 app/BaseController.php 添加OPTIONS处理

    // 初始化
    protected function initialize()
    {
        if ($this->request->isOptions()) {
            exit();
        }
    }

增加自定义Header

修改文件 vendor/topthink/framework/src/think/middleware/AllowCrossDomain.php 在末尾增加 access_token

    protected $header = [
        'Access-Control-Allow-Credentials' => 'true',
        'Access-Control-Allow-Methods'     => 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers'     => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With, access_token',
    ];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容