[英]How to receive response from API in Angular 4
我在Angular 4應用程序中工作,試圖從API獲取數據,在此我不知道我是否正在接收響應,但是我沒有收到任何錯誤。
這就是我嘗試過的...
ngOnInit() {
this.http.get(`url=${this.product_Name}`)
.subscribe(data => { this.path = data[0].Image_PATH_NAME;}, error => console.error(error));
}
}
這是我從API獲得的JSON響應
[{"Image_PATH_NAME":"assets/Images/Product_Details_Page/Show101.png"}]
在調試上述代碼時,我看不到任何響應。我是Angular4的新手,請引導我獲取API響應。
看起來您正在使用舊的HttpModule
在這種情況下,必須在使用前將響應轉換為JSON:
ngOnInit() {
this.http.get(`url=${this.product_Name}`)
.map(res => res.json())
.subscribe(data => { this.path = data[0].Image_PATH_NAME;}, error => console.error(error));
}
}
如果你通過
this.path = data[0].Image_PATH_NAME;
這表示您的http通話已正確完成。 當您使用訂閱時,第一個參數是調用正確完成時調用的函數,第二個參數是發生錯誤時調用的函數。
您還可以在devtools中檢查“網絡”部分,並查找http呼叫以查看獲得的內容
希望能有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.