2016-11-21 4 views
1

Dieses ist von Winkel 2 http Führung für app/toh/hero.service.ts:In Angular 2 was macht response.json()?

... 
@Injectable() 
export class HeroService { 
    private heroesUrl = 'app/heroes'; // URL to web API 
    constructor (private http: Http) {} 
    getHeroes(): Observable<Hero[]> { 
    return this.http.get(this.heroesUrl) 
        .map(this.extractData) 
        .catch(this.handleError); 
    } 
    private extractData(res: Response) { 
    let body = res.json(); 
    return body.data || { }; 
    } 
    private handleError (error: Response | any) { 
    ... 
    } 

} 

verweisen auf die Linie lassen body = res.json(); Von API konnte ich keine json() -Methode auf Response-Objekt finden. Von Antwortquelle ich das finde:

export var Body = (function() { 
    function Body() { 
    } 
    /** 
    * Attempts to return body as parsed `JSON` object, or raises an exception. 
    */ 
    Body.prototype.json = function() { 
     if (isString(this._body)) { 
      return Json.parse(this._body); 
     } 
     if (this._body instanceof ArrayBuffer) { 
      return Json.parse(this.text()); 
     } 
     return this._body; 
    }; 

Wie diese 2 verwandt sind?

+0

Unklar ist? Die Methode '.json' versucht, den Antworttext in JS-Objekte für Sie zu analysieren. Es wird [hier] (https://angular.io/docs/ts/latest/guide/server-communication.html#!#parse-to-json) erwähnt, dass * "Der Angular HTTP-Client folgt der Fetch-Spezifikation" *. – jonrsharpe

+0

Es gibt die geparsten Daten der Antwort zurück ... – Maxime

+0

@johnsharpe, Wahrscheinlich habe ich meine Frage nicht richtig formuliert. Ich wollte wissen, wo in 'Response' Quellcode kann ich json() -Methode finden. Ich finde die Methode im 'Body' Quellcode. Ich beantworte meine eigene Frage, bitte lesen Sie sie durch, und wenn Sie ein Missverständnis von mir feststellen, weisen Sie bitte darauf hin. –

Antwort

3

Ich sah in node_modules/@ Winkel/http/src & gehalten für die Suche, dass in der Datei static_response.js

Export var Antwort

gefunden. Dort heißt es:

export var Response = (function (_super) { 
__extends(Response, _super); 
function Response(responseOptions) { 
    _super.call(this); 
    this._body = responseOptions.body; 
    this.status = responseOptions.status; 
    this.ok = (this.status >= 200 && this.status <= 299); 
    this.statusText = responseOptions.statusText; 
    this.headers = responseOptions.headers; 
    this.type = responseOptions.type; 
    this.url = responseOptions.url; 
} 
Response.prototype.toString = function() { 
    return "Response with status: " + this.status + " " + this.statusText + " for URL: " + this.url; 
}; 
return Response; 
}(Body)); 

in der gleichen Datei __extends ist wie folgt definiert:

var __extends = (this && this.__extends) || function (d, b) { 
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 
    function __() { this.constructor = d; } 
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 
}; 

So Körper hat json() & Antwort aus Körper mittels Kopieren bekommt.

-1

etwas wie folgt aus:

JSON.parse(data['_body']) 
0

Sie Ihre Antwort mit json Karte()

this.jsonp.get("http://localhost:8080/api/getpost") 
      .map(res => res.json()) 
      .subscribe(data => console.log(data)); 
Verwandte Themen