美文网首页
vue+axios每次都重复请求两次解决

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

作者: Yuge_ | 来源:发表于2020-12-17 20:09 被阅读0次

使用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',
    ];

相关文章

网友评论

      本文标题:vue+axios每次都重复请求两次解决

      本文链接:https://www.haomeiwen.com/subject/hkftnktx.html