http - How to perform callback in angular2? -
i have following code in angular2 service:
this.returned: string; parseresponse() : void { console.log("response:",this.returned); } sendhttp(text: string): void { var to_send = json.stringify({"text": text}); var headers = new headers(); headers.append('content-type', 'application/json'); this.http .post('http://localhost:8080/', to_send, { headers: headers }) .map(res => { this.parseresponse() }) .subscribe( function(response) { console.log("success response" + response)}, function(error) { console.log("error happened" + error)}, function() { parseresponse(); } ); }
but unfortunately parseresponse()
not running after nodejs server returns "testing". however, request received correctly. me please?
edit:
this actual code. nothing console.logged. request sent. no callbacks executed.
parseresponse(res: response) : void { console.log("response:",res); } sendhttp(text: string): void { var to_send = "sending"; var headers = new headers(); headers.append('content-type', 'application/json'); this.http .post('http://localhost:8080/', to_send, { headers: headers }) .map((res) => res.json() ) .subscribe( (response) => { console.log("success response" + response)}, (error) => { console.log("error happened" + error)}, () => { this.parseresponse(res); } ); }
you want either pass response method:
.map(res => this.parseresponse(res))
or pass method reference:
.map(this.parseresponse)
also parseresponse
method should return "parsed" response:
parseresponse(res: response): any{ let jsonres = res.json(); console.log("response:", jsonres); return jsonres; }
Comments
Post a Comment