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

使用Thinkphp6 开启全局跨域

修改文件 app/middleware.php 增加 \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每次都重复请求两次解决)