8080 - 端口,其中后端托管 4200 - 我Angular2前端代理配置不具有角CLI工作
在我Angular2的项目,我有文件proxy.config.json像这样
{
"/api": {
"target": "http://localhost:8080",
"secure": false,
"changeOrigin": "true",
"pathRewrite": {"^/api": ""}
}
}
内容
在Angular2的package.json我改变启动过程来"start": "ng serve --proxy-config proxy.config.json"
当我内部指挥官npm start
键入然后在开始时,我可以看到代理创建的:/ API - >http://localhost:8080。那么,我认为迄今为止是好的。
我试图发送一个请求(Angular2)
constructor(private http: Http) {
this.getUsers();
}
getUsers(): any {
return this.http.get("/api/getusers")
.subscribe(response => {
console.log(response);
})
}
我得到一个错误,http://localhost:4200/api/getusers 404(未找到)。正如我们所看到的,没有任何东西被代理。为什么?我做错什么了吗?的Visual Studio代码
控制台输出
10% building modules 2/2 modules 0 active[HPM] Proxy created: /api/ -> http://localhost:8080
[HPM] Proxy rewrite rule created: "^/api" ~> ""
[HPM] Subscribed to http-proxy events: [ 'error', 'close' ]
Hash: d102bcd0909a1776c844
Time: 27041ms
chunk {0} main.bundle.js, main.bundle.map (main) 13.6 kB {2} [initial] [rendered]
chunk {1} styles.bundle.js, styles.bundle.map (styles) 130 kB {3} [initial] [rendered]
chunk {2} vendor.bundle.js, vendor.bundle.map (vendor) 3.87 MB [initial] [rendered]
chunk {3} inline.bundle.js, inline.bundle.map (inline) 0 bytes [entry] [rendered]
webpack: Compiled successfully.
[HPM] Rewriting path from "/api/getusers" to "/getusers"
[HPM] GET /api/getusers ~> http://localhost:8080
这是浏览器的控制台响应:
GET http://localhost:4200/api/getusers 404 (Not Found)
error_handler.js:54 EXCEPTION: Response with status: 404 Not Found for URL: http://localhost:4200/api/getusers
Subscriber.js:238 Uncaught Response {_body: "<html><head><title>Apache Tomcat/7.0.47 - Error re…hade"><h3>Apache Tomcat/7.0.47</h3></body></html>", status: 404, ok: false, statusText: "Not Found", headers: Headers…}
有你试着用完整的URL的http:/ /本地主机:4200/API/getusers而不是/ API/getusers – IsuruAb
我的后端托管在本地主机:8080/API这就是为什么我使用代理服务器设置,以及本地主机:8080/API/getusers工作的罚款。 –
控制台输出是什么? – IsuruAb