• laravel 5.5 跨域问题 并且laravel的跨域 Access-Control-Allow-Origin 报错的坑


    laravel 5.5 跨域问题

    在laravel的中间件(app/Http/Middleware)中添加新文件

      可以用命令创建中间件

    php artisan make:middleware 名字

      并在新创建的文件中修改 handle 方法为:
     

     public function handle($request, Closure $next)
      {
    
        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'false');
        return $response;
      }

      且在 app/Http/Kernel.php中 的 $middleware 中添加

    AppHttpMiddleware名字::class,

      跨域才解决成功。

      有一个很大的坑:就是laravel在跨域访问时,用 dd() 打印会直接报错,前端控制台报错为

    Failed to load url: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'url' is therefore not allowed access. The response had HTTP status code 500.
  • 相关阅读:
    scrapy.FormRequest与FormRequest.from_response 的区别
    可迭代对象和生成器
    css选择器
    xlwt写入excel时候的合并单元格
    html form提交的几种方式
    Python decorator 拦截器
    python manage.py makemigrations & migrate
    asianux4.5 环境, 本地yum源,局域网yum源的配置
    mysql基本操作
    sqlite-mysql migrate
  • 原文地址:https://www.cnblogs.com/wmmznb/p/9681222.html
Copyright © 2020-2023  润新知