美文网首页
axios 搭配laravel无法跨域问题解决

axios 搭配laravel无法跨域问题解决

作者: _不能说的秘密i | 来源:发表于2019-01-31 12:10 被阅读33次

https://learnku.com/laravel/t/12584/unable-to-cross-domain-axios-get-x-csrf-token-does-not-allow-access-control-allow-headerss-preflight-response

https://learnku.com/articles/6504/laravel-cross-domain-solution

<?php
namespace App\Http\Middleware;
use Closure;
class EnableCrossRequestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $origin = $request->server('HTTP_ORIGIN') ? $request->server('HTTP_ORIGIN') : '';
        $allow_origin = [
            'http://localhost:8000',
        ];
        if (in_array($origin, $allow_origin)) {
            $response->header('Access-Control-Allow-Origin', $origin);
            $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
            $response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
            $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
            $response->header('Access-Control-Allow-Credentials', 'true');
        }
        return $response;
    }
}

相关文章

网友评论

      本文标题:axios 搭配laravel无法跨域问题解决

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