zoukankan      html  css  js  c++  java
  • Laravel 5.7 No 'Access-Control-Allow-Origin' header is present on the request resource

    前后端项目跨域访问时会遇到此问题,解决方法如下:

    创建一个中间件

    php artisan make:middleware EnableCrossRequestMiddleware
    

    该中间件的文件路径为:app/Http/Middleware/EnableCrossRequestMiddleware.php

    中间件 EnableCrossRequestMiddleware 内容如下:

    <?php
    /**
     * 跨域设置
     */
    
    namespace AppHttpMiddleware;
    
    use Closure;
    use IlluminateHttpResponse;
    
    class EnableCrossRequestMiddleware
    {
        /**
         * Handle an incoming request.
         *
         * @param  IlluminateHttpRequest  $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 = config('origin.allowed');
    
            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;
        }
    }
    

    app/Http/Kernal.php 文件中将其注册为全局中间件

    namespace AppHttp;
    
    use AppHttpMiddlewareAuthenticateMain;
    use AppHttpMiddlewareAuthenticateSeller;
    use AppHttpMiddlewareAuthenticateUser;
    use IlluminateFoundationHttpKernel as HttpKernel;
    
    class Kernel extends HttpKernel
    {
        /**
         * The application's global HTTP middleware stack.
         *
         * These middleware are run during every request to your application.
         *
         * @var array
         */
        protected $middleware = [
            AppHttpMiddlewareCheckForMaintenanceMode::class,
            IlluminateFoundationHttpMiddlewareValidatePostSize::class,
            AppHttpMiddlewareTrimStrings::class,
            IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
            AppHttpMiddlewareTrustProxies::class,
            AppHttpMiddlewareEnableCrossRequestMiddleware::class, // 添加这一行将其注册为全局中间件
        ];
    }
    

    增加配置文件app/config/origin.php,内容为允许的域名

    <?php
    return [
    
        'allowed' => [
            'http://test.example.com',
            'http://test-fe.example.com:8080'
        ]
    
    ];
    

    PS - 个人博客原文:Laravel 5.7 No 'Access-Control-Allow-Origin' header is present on the request resource

  • 相关阅读:
    css雪碧图生成工具4.3更新
    移动端webapp自适应实践(css雪碧图制作小工具实践)图文并茂
    css雪碧图生成工具4.2更新
    手机端页面rem自适应脚本
    css雪碧图生成工具4.1更新
    V4.0到来了,css雪碧图生成工具4.0更新啦
    css sprite,css雪碧图生成工具V3.0更新
    css sprite css雪碧图生成工具
    CSS3 Loading(加载)动画效果
    js new
  • 原文地址:https://www.cnblogs.com/feiffy/p/9765316.html
Copyright © 2011-2022 走看看