Marco Castano Marco Castano - 4 months ago 34
JSON Question

Extract data from respone Angular 2

I have 1 component and 1 service.

Component element :

...
constructor(private requestService : RequestService)
{

}
ngOnInit()
{
this.a = this.requestService.send_request(urlWebAPI);
console.log(this.a);
}
...


Service:

constructor(private http: Http)
{
this.http = http;
this.headers = new Headers
({
'Content-Type': 'application/json'
});
}

send_request(additional_url: String)
{
return this.http.post(this.url + additional_url, {headers: this.headers})
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
private handleError(error: any)
{
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}


But this is what I get printed...How I can directly extract 'listaUniversita' array from this request?
enter image description here

Answer
send_request(additional_url: String) 
{       
    return this.http.post(this.url + additional_url, {headers: this.headers})
        .toPromise()
        .then(response => response.json().data)                    //<----changed it
        .catch(this.handleError);
}


ngOnInit()
{
    this.requestService.send_request(urlWebAPI).then((result) => {  //<----changed it
       this.a=result;
    )};
}
Comments