Angular 后端的角度代理不工作?

Angular 后端的角度代理不工作?,angular,proxy,angular-cli,Angular,Proxy,Angular Cli,我一步一步地做了angular站点中提到的所有事情,但仍然没有代理请求 8080-我的springboot应用程序和后端4200-我的Angular2前端 在Angular2项目中,我有proxy.cons.json文件,内容如下: { "/api": { "target": "http://localhost:8080", "secure": false, "changeOrigin": true, "logLevel": "debug" } }

我一步一步地做了angular站点中提到的所有事情,但仍然没有代理请求

8080-我的springboot应用程序和后端4200-我的Angular2前端

在Angular2项目中,我有proxy.cons.json文件,内容如下:

{
  "/api": {
     "target": "http://localhost:8080",
     "secure": false,
     "changeOrigin": true,
     "logLevel": "debug"
  }
}
在Angular2 package.json中,我将启动过程更改为“start”:“ng serve--proxy config proxy.conf.json”

当我在commander npm start中键入时,我可以看到创建的代理:/api->。嗯,我想到目前为止还不错

我正在尝试发送请求(Angular2)

下面是我的service.ts文件

   import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';

@Injectable()
export class SpipService 
{

  constructor(private http: Http) { }

  callRest():  Promise<string> {
    let context:String =  window.location.pathname.split('/').slice(0,-1).join('/');
    console.log("Please check--------------->");
    return this.http.get("/api/serviceContext/rest/hello/")
      .toPromise()
      .then(response => response.text() as string)
      .catch(this.handleError);
  }

    private handleError(error: any): Promise<any> {
    console.error('Some error occured', error);
    return Promise.reject(error.message || error);
  }
}
我得到一个404(未找到)的错误。正如我们所看到的,没有任何代理。为什么?


说清楚。当我手动操作时,一切正常。请引导。

您的JSON应该是

{
  "/api": {
     "target": "http://localhost:8080",
     "secure": false,
     "changeOrigin": true,
     "logLevel": "debug"
  }
}
让你的电话看起来像这样

return this.http.get("/api/serviceContext/rest/hello/"). 
编辑我自己的配置:

{
  "/api": {
    "target": "http://private-url-your-peeker:8765/",
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}
我的电话

return this.http.get("api/serviceContext/rest/hello/"). 

在我通过
proxy.conf.json
进行如下修改之后,所有的事情似乎都运行良好。希望它能帮助将来的人:

{
  "/api/*": {
    "target": "http://localhost:8080",
    "secure": false,
    "pathRewrite": {"^/api" : ""},
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

js:2935 GET 404(未找到)这次,请允许我怀疑你,因为我眼前有相同的代码,它可以工作。你确定你没有做其他事情吗?我刚刚用你说的更新了我的问题。如果我做错了什么,请告诉我。现在似乎没有任何问题。你确定你的端点已经启动了吗?但是如果你看到url,它仍然需要4200。。是的,它起来了。。不应该是4200知道吗??。
{
  "/api/*": {
    "target": "http://localhost:8080",
    "secure": false,
    "pathRewrite": {"^/api" : ""},
    "changeOrigin": true,
    "logLevel": "debug"
  }
}