请求的资源中缺少“Access-Control-Allow-Origin”头信息,响应的HTTP状态码为403。

4
我有 Angular 产品服务和使用 HttpClient 方法调用 "http://jsonplaceholder.typicode.com/posts" URL。
但是,它显示 "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5700' is therefore not allowed access. The response had HTTP status code 403."。
根据我的理解,以上错误需要设置头文件,但这只是公共获取 API,所以我认为不需要为此设置头文件。
提前致谢。

无论是公共API还是非公共API,头信息都需要设置。 - anthony
你能提供一个使用 https://stackblitz.com/fork/angular 的示例吗? - Andy Danger Gagne
1个回答

4

这实际上是由浏览器抛出的错误,只能在服务器端进行修复。正如您所看到的,终端点没有在响应头中设置Access-Control-Allow-Origin,因此您无法从浏览器内的任何其他域调用它(在本例中,'http://localhost:5700')。它的响应头是:

access-control-allow-credentials →true
cache-control →public, max-age=14400
cf-cache-status →HIT
cf-ray →3c4f9790845896e8-FRA
connection →keep-alive
content-encoding →gzip
content-type →application/json; charset=utf-8
date →Tue, 28 Nov 2017 19:04:16 GMT
etag →W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
expires →Tue, 28 Nov 2017 23:04:16 GMT
pragma →no-cache
server →cloudflare-nginx
transfer-encoding →chunked
vary →Accept-Encoding
via →1.1 vegur
x-content-type-options →nosniff
x-powered-by →Express

感谢GHB的反馈,它澄清了我的误解:) - ranjit redekar
很高兴能帮忙 ;) - GHB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接