[英]Angular Proxy Configuration
我有一個基本問題,但我無法解決這個問題。 我想從我的 angular 應用程序連接到 git hub api,但總是收到 400(錯誤請求)。
我做了什么:
package.json
- 我添加/編輯了"start": "ng serve --proxy-config proxy.config.json"
proxy.config.json
在這個文件中,我在/api
配置下面添加了
{ "/api": { "target": "https://api.github.com/", "secure": false } }
在我的someComponent.ts
組件中,我添加了:
import { HttpClient } from '@angular/common/http';
constructor(private http:HttpClient){}
somefunc(){ return this.http.get("/api/users?per_page=10") .subscribe(data =>{ this.datas = data console.log(this.datas) }) }
但是當我嘗試在控制台中執行這個 get 方法時,我收到了:
GET http://localhost:4200/api/users?per_page=10 400(錯誤請求)
和
錯誤 HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: " http://localhost:4200/api/users?per_page=10 ", ok: false, ...}
有人可以幫我做錯什么嗎?
對於那些將有類似問題的人。 如果您的 api 未托管在 localhost 上(如我的示例中的外部 api),您需要在proxy.config.json
添加以下proxy.config.json
要添加的代碼:
"pathRewrite": { "^/api": "" },
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.