Angular 2 localhost和Backendserver连接 [英] Angular 2 localhost and Backendserver Connection

查看:125
本文介绍了Angular 2 localhost和Backendserver连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的机器上使用npm start运行Angular 2项目并访问另一台服务器上的后端。
当我调用帖子或获取时,我总是在Firefox控制台中收到Access-Control-Allow-Origin missing错误。
当我在服务器上部署我的应用程序时,它运行得非常好,但我不想等待所有时间来部署我的应用程序。

I am trying to run a Angular 2 project with npm start on my machine and access the backend on another server. When I call a post or get I always getting the 'Access-Control-Allow-Origin missing' error in the Firefox console. When I deploy my app on the server it works perfectly fine, but I dont want to wait all the time to deploy my app.

我尝试了不同的解决方案,就像代理人一样。我是否需要配置本地主机或其他东西?是否有最先进的解决方案?

I tried different solutions, like proxies. Do I need to configure my localhost or something else? Is there a state of the art solution?

推荐答案

您可以在本地环境中设置代理。

You can set up proxy in your local environment.

package.json 中添加脚本serve-dev:< startApplication> - sourcemap = false --proxy-config proxy.config.json

使用 npm run serve-dev 运行它。

proxy.config.json 文件应如下所示:

{
  "/api/*":{
    "target":"http://localhost:5005",
    "secure": false,
    "logLevel": "debug"
  }
}

当你在你的网站中调用端点时服务只需获取应该是: this._http.get('。/ api / myEndpoint')

And when you call endpoint in your service just get should be like: this._http.get('./api/myEndpoint').

这篇关于Angular 2 localhost和Backendserver连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆