Angular 2 能够 post 请求但无法读取远程资源

Angular 2 able to post request but not able to read the remote resource

我尝试 post 向我的 Laravel 后端发出请求,但由于错误 "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8000/path. (Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel)" 显示而未能得到响应。

我在我的客户端和服务器中都添加了 Access-Control-Allow-Origin 和 Access-Control-Allow-Headers。为什么错误仍然显示?

删除您添加的 client-side 代码以在请求中发送 Access-Control-Allow-Origin

(Reason: missing token ‘access-control-allow-origin’ in CORS header ‘Access-Control-Allow-Headers’ from CORS preflight channel)" showed.

I have added Access-Control-Allow-Origin and Access-Control-Allow-Headers in both my client and server. Why does the error still showing?

显示错误是因为您向 client-side 请求添加了 Access-Control-Allow-Origin header。但是Access-Control-Allow-Origin是服务器响应header。因此,如果您在请求中从客户端发送它,您将看到您现在看到的那种错误。

“Missing token 'access-control-allow-origin' in CORS header 'Access-Control-Allow-Headers'“ 是浏览器告诉你你的客户端代码是发送 Access-Control-Allow-Origin 请求 header,但服务器返回的 Access-Control-Allow-Headers 响应 header 没有将“Access-Control-Allow-Origin”列为 header 它允许在请求中使用的名称。

因此,浏览器不会让您的前端 JavaScript 代码读取响应。