【发布时间】:2017-01-31 12:27:28
【问题描述】:
所以我从 Angular 2 中学习了英雄教程,将它与 angular-cli 项目一起使用。运行 ng serve 并在内存 api 中使用,它可以工作。
将服务器 url 更改为指向我的提供 rest api 的 tomcat 服务器。我用 curl 对此进行了测试,它会返回数据。
然后我将其构建用于生产,并将生成的包放入与 tomcat 相同的机器上的 nginx 中。
在 Firefox 中运行,得到正确的页面但没有数据。控制台显示 GET 正常。但我确实得到了
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://centos7:8080/heroes/heroes. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
StackOverFlow 中关于这个主题的其他问题说两者必须在同一个域中,它们是,tomcat 服务器是 centos7:8080/heroes/heroes 和 nginx 服务器是 centos7/
我应该打开 CORS 吗?我看过http://enable-cors.org/server_nginx.html所以我需要在nginx.conf中添加这个?
有人有什么想法吗?
问候
【问题讨论】:
-
centos7/ 和 centos7:8080/ 不是同一个域。是的,港口很重要。
-
那么我需要设置CORS吗?
-
所以我将上面链接中的代码添加到 /etc/nginx/nginx.conf 的 location / { } 部分并重新加载了 nginx ,它没有任何区别。与之前的控制台输出完全相同。我做错了什么?