简体   繁体   中英

How to receive response from API in Angular 4

I am working in an Angular 4 application,Trying to get data from API,In this I don't know whether I am receiving the response or not ,But I don't receive any error.

This is what I have tried...

ngOnInit() {
    this.http.get(`url=${this.product_Name}`)
     .subscribe(data => { this.path = data[0].Image_PATH_NAME;}, error => console.error(error));
     } 
 } 

This is the JSON response I got from API

[{"Image_PATH_NAME":"assets/Images/Product_Details_Page/Show101.png"}]

While debugging the above lines I can't see any kind of response .I am new to Angular4 please guide me to get API response.

This is the answer for this question.

.subscribe(data => {
      console.log(data[0]['Image_PATH_NAME']);
    });

console view.

在此处输入图片说明

It looks like you are using the old HttpModule in which case, you have to convert your response to JSON before use :

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));
     } 
 } 

if you pass in

this.path = data[0].Image_PATH_NAME;

it means your http call finished correctly. When you use subscribe, the first parameter is a function called when the call finished correctly, the second parameter is a function called when something went wrong.

You can also check the network section in you devtools and look for you http call to see what you got

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM