Angular 2: How to access an HTTP response body?

后端 未结 10 2060
南方客
南方客 2020-12-13 12:25

I wrote the following code in Angular 2:

this.http.request(\'http://thecatapi.com/api/images/get?format=html&results_per_page=10\').
      subscribe((re         


        
10条回答
  •  情深已故
    2020-12-13 13:03

    Here is an example to access response body using angular2 built in Response

    import { Injectable } from '@angular/core';
    import {Http,Response} from '@angular/http';
    
    @Injectable()
    export class SampleService {
      constructor(private http:Http) { }
    
      getData(){
    
        this.http.get(url)
       .map((res:Response) => (
           res.json() //Convert response to JSON
           //OR
           res.text() //Convert response to a string
       ))
       .subscribe(data => {console.log(data)})
    
      }
    }
    

提交回复
热议问题