Skip to content Skip to sidebar Skip to footer

Javascript / Angular Httpclient Get Data From Url And Pass Outside Method

I am using angular 5 and I have installed the httpClient Module so I can get data from an api. On my app.component.ts I have this: getData() { this.http.get('url to get xml dat

Solution 1:

Modify your first method to simply return an async result from your API call, then subscribe to the method itself.

getData() {
    returnthis.http.get('url to get xml data from', { responseType: 'text' });
}

and in you ngOnInit

ngOnInit() {
    this.getData().subscribe(res => {
      this.result = res;
   });
}

Solution 2:

getData() {
    returnthis.http.get('url to get xml data from', { responseType: 'text' });
}


 ngOnInit() {
this.getData().subscribe(data => {
    this.result = data;
 });
}

Solution 3:

     getData() {
   returnthis.http.get('url to get xml data from', {
     responseType: 'text'
   });
 }
 ngOnInit() {
   this.getData().subscribe(data => {
     this.result = data;
   });
 }

Post a Comment for "Javascript / Angular Httpclient Get Data From Url And Pass Outside Method"