laravel使用中出现的问题

博客分类: 笔记 阅读次数: comments

laravel使用中出现的问题

跨域:

​ 在前后端分离的写法中,最重要的就是处理跨域问题,跨域解决不了,什么都没法做.

一般可以使用添加一个中间件来解决:

在laravel中使用 命令 php artisan make:middleware 中间件名

在中间件中写入代码

public function handle($request, Closure $next)
    {
        $response = $next($request);
        $response->header('Access-Control-Allow-Origin', '*');
        $response->header('Access-Control-Allow-Headers', 'Origin,No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, token');
        $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
        $response->header('Access-Control-Allow-Credentials', 'true');
        return $response;
    }

在app/Http/Kernel.php文件中注册中间件,因为跨域问题是每条路由都会有的问题,所以我直接注册的全局中间件,当然也可以在路由中使用

接下来就可以解决跨域问题了.