Getting "Multiple CORS header ‘Access-Control-Allow-Origin’ not allowed" on my Laravel API from Angular

huangapple go评论55阅读模式
英文:

Getting "Multiple CORS header ‘Access-Control-Allow-Origin’ not allowed" on my Laravel API from Angular

问题

我使用Laravel开发了一个API,与Postman一起使用时运行完美,现在正在使用Angular开发其前端。

每次我发送请求到API时,我都会收到这个错误消息:跨源请求被阻止:同源策略禁止读取远程资源 http://localhost/master-fullstack/api-rest-laravel/public/api/user/update。(原因:不允许多个CORS标头‘Access-Control-Allow-Origin’)。

我已经在我的Laravel项目中正确设置了CORS:

index.php

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
header("Allow: GET, POST, OPTIONS, PUT, DELETE");

这是我在Angular项目中的设置:

user.service.ts

update(token: string, user: any) {
  let json = JSON.stringify(user);
  let params = "json="+json;
  let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded').set('Authorization', token);
  return this._http.put(this.url + 'user/update', params, {headers: headers});
}

看起来问题似乎是关于具有多个Access-Control-Allow-Origin标头,但我只有一个,因为如果我删除它,我会收到另一个错误:CORS标头‘Access-Control-Allow-Origin’丢失

我还尝试过用http://localhost:4200替换*,但问题仍然存在!

我不明白发生了什么。

谢谢。

已解决!我不得不将此代码添加到httpd.conf

<IfModule mod_headers.c>
  Header set Access-Control-Allow-Origin "*"
</IfModule>

然后,我在Apache上启用了headers模块(WampServer -> Apache -> Apache Modules -> headers_module)。

英文:

I've developed an API with Laravel which worked perfect with Postman, and I'm now developing its front-end with Angular.

I'm currently getting this error each time I send a request to the API: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/master-fullstack/api-rest-laravel/public/api/user/update. (Reason: Multiple CORS header ‘Access-Control-Allow-Origin’ not allowed).

I already have set CORS properly in my laravel project:

index.php

header(&#39;Access-Control-Allow-Origin: *&#39;);
header(&quot;Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, Authorization&quot;);
header(&quot;Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE&quot;);
header(&quot;Allow: GET, POST, OPTIONS, PUT, DELETE&quot;);

This is what I have in my Angular project:

user.service.ts

update(token: string, user: any) {
  let json = JSON.stringify(user);
  let params = &quot;json=&quot;+json;
  let headers = new HttpHeaders().set(&#39;Content-Type&#39;, &#39;application/x-www-form-urlencoded&#39;).set(&#39;Authorization&#39;, token);
  return this._http.put(this.url + &#39;user/update&#39;, params, {headers: headers});
}

It looks like the issue is about having multiple Access-Control-Allow-Origin headers, but I just have one, because if I remove it, I get this another error: CORS header ‘Access-Control-Allow-Origin’ missing

I have also tried replacing * for http://localhost:4200, but the issue still persists!

I don't understand what's going on.

Thanks.

Fixed! I've had to add this code to httpd.conf.

&lt;IfModule mod_headers.c&gt;
  Header set Access-Control-Allow-Origin &quot;*&quot;
&lt;/IfModule&gt;

And then, I've enabled headers module on Apache (WampServer -> Apache -> Apache Modules -> headers_module).

答案1

得分: 1

你需要将以下内容添加到你的请求头中:

"Accept": "application/json"
英文:

you have to add

&quot;Accept&quot;: &quot;application/json&quot;

to your request headers

huangapple
  • 本文由 发表于 2023年6月1日 23:58:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定