一、设置Laravel
1、
建立中间件Cors.php
命令:php artisan make:middleware Cors
在/app/Http/Middleware/ 目录下会出现一个Cors.php 文件。
2、
在handle 方法中加入如下内容:
$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;
3、在 Kernel.php文件中的$middleware中加入刚刚添加的中间件:\App\Http\Middleware\Cors::class,
如:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
二、前端设置,为dev环境添加代理
修改config下index.js
proxyTable: { '/api': 'http://localhost:9000' },
参考:
http://blog.csdn.net/leedaning/article/details/53787008
http://blog.csdn.net/z852064121/article/details/75460408
https://segmentfault.com/q/1010000007697635?_ea=1425764

本文介绍如何在Laravel应用中配置跨域资源共享(CORS),包括创建中间件和设置响应头;同时,还提供了前端开发环境中通过配置代理解决跨域问题的方法。

1090

被折叠的 条评论
为什么被折叠?



