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