Angular2 /的NodeJS:使用在前端角2 &为的NodeJS后端

无法从后端数据localhost上

问题描述:

我写了一个小程序的SPA我在我的电脑上启动两个后台&前端(http://localhost:3000 & http://localhost:4200Angular2 /的NodeJS:使用在前端角2 &为的NodeJS后端</p> <p>无法从后端数据localhost上

从后端当角试图获取数据上升了一个错误:

XMLHttpRequest cannot load http://localhost:3000/mailboxes . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:4200 ' is therefore not allowed access.

EXCEPTION: Uncaught (in promise): 0 - {"isTrusted":true} ...

如何使这项工作?

+1

读[这](HTTPS:/ /developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) –

您的服务和客户端在两个不同的端口上运行,因此就您的服务而言,此请求来自外部位置。 所以你需要让你的服务方知道你允许这个请求。

要做到这一点,你需要在你的服务 https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

这里启用跨源资源共享是链接到一个NPM CORS库非常容易使用 https://www.google.co.za/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=npm+cors

+0

它有帮助。非常感谢。 –