【发布时间】:2017-03-07 17:10:56
【问题描述】:
我开发了一个 Angular 2 应用程序,它应该从外部服务器请求 api,但也向本地 Apache 服务器请求。使用 jsonp 请求外部 api 并且工作正常。对于本地/内部服务器,我想使用 http 模块,因为这两个应用程序将在同一台服务器上运行。
我的问题是,在开发 Angular 时实际运行在端口 3000 上,而 Apache 在端口 80 上运行。如果我只将路径设置为(例如 /some/path)Angular 被请求到端口 3000,而没有这样的部分成立。所以我设置了完整的地址(http://localhost/some/path),但这会被检测为跨源请求并且是不允许的。
XMLHttpRequest cannot load http://localhost/gizmo/default-questions/get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我怎么能告诉 Angular 常规 http 端口将是 80。我认为将来所有的都将构建它不会有问题,不是吗?但我不想为所有更正而构建和替换它。这可能吗?
【问题讨论】:
-
你需要配置本地服务器,而不是Angular
-
如果端口不同,它已经是 CORS。您需要配置服务器以允许 CORS 请求。
标签: angular angular2-http