【问题标题】:Angular2 in nginx - CORS header 'Access-Control-Allow-Origin' missingnginx 中的 Angular2 - 缺少 CORS 标头“Access-Control-Allow-Origin”
【发布时间】: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 ,它没有任何区别。与之前的控制台输出完全相同。我做错了什么?

标签: nginx angular cors


【解决方案1】:

所以我不是,如果这是绝对答案,但它删除了浏览器控制台中的 CORS 错误。

在我的 grails 3 rest api 应用程序中,我添加了 grails 3 COR 拦截器https://github.com/appcela/grails3-cors-interceptor

我现在遇到了一个不同的问题,但我将在另一个问题中提出。

【讨论】:

    猜你喜欢
    • 2018-03-28
    • 2019-02-07
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2018-09-19
    • 2020-01-05
    相关资源
    最近更新 更多