Laravel POST 请求 Cors 没有“Access-Control-Allow-Origin”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55114988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel POST request Cors No 'Access-Control-Allow-Origin'
提问by kalenpw
Currently receiving an error message when I attempt to POST to a given url. GET works fine on the same domain here is my error:
当我尝试 POST 到给定的 url 时,当前收到一条错误消息。GET 在同一个域上工作正常,这是我的错误:
Access to XMLHttpRequest at 'http://localhost:8000/api/users/login' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
CORS 策略阻止了在“ http://localhost:8000/api/users/login”处访问 XMLHttpRequest来自源“ http://localhost:8080”:对预检请求的响应未通过访问控制检查:否请求的资源上存在“Access-Control-Allow-Origin”标头。
I've found countless questions documenting this same issue all with variations of modify my Cors middleware.
我发现无数问题记录了同样的问题,所有这些问题都包含修改我的 Cors 中间件的变体。
Currently my CORS middleware looks like this:
目前我的 CORS 中间件如下所示:
class Cors
{
public function handle($request, Closure $next)
{
if ($request->isMethod('OPTIONS')) {
$response = Response::make();
} else {
$response = $next($request);
}
return $response
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With, Application');
}
}
The error states No 'Access-Control-Allow-Origin' which I believe my ->header('Access-Control-Allow-Origin', '*')
should handle since the * is a wild card from my understanding. Is there a different issue at play here I'm missing? Stack is Laravel backend with VueJS front end using Axios for HTTP requests if that is relevant.
错误指出 No 'Access-Control-Allow-Origin' 我相信我->header('Access-Control-Allow-Origin', '*')
应该处理它,因为 * 是我理解的通配符。我错过了这里有什么不同的问题吗?Stack 是 Laravel 后端,VueJS 前端使用 Axios 进行 HTTP 请求(如果相关的话)。
Edit: This question is unique in that I already have the headers suggested in most answers specifically:
编辑:这个问题是独一无二的,因为我已经在大多数答案中特别推荐了标题:
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
and am still getting the error.
并且仍然收到错误。
回答by Yannick senga romeo
Here are two packages which can help you handling CORS in Laravel, you can choose one
这里有两个包可以帮助你在 Laravel 中处理 CORS,你可以选择一个