跨域问题
来源:8-13 商城前端分类API开发-支持无限极分类
慕粉慕粉1888
2020-02-08
老师目前我遇到这个问题,提示跨域:
‘http://127.0.0.1:8999/api/index/cagegoryGoodsRecommend’ from origin ‘http://127.0.0.1:8998’ has been blocked by CORS policy:
http://127.0.0.1:8999 是我的api地址
http://127.0.0.1:8998 是vue部署地址
我在vue nginx配置文件做了proxy_pass 配置,以下是截取的部分配置
server {
listen 8998;
server_name 127.0.0.1;
location ^~ /api/ {
proxy_pass http://127.0.0.1:8999;
}
这样不成功,这个配置该怎么修改呢?谢谢
写回答
1回答
-
慕码人6234946
2020-02-09
可以写一个中间件
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; }
022021-03-14
相似问题