angularjs - Angular 2 - simple HTTP GET from component not working -
i have angular 2 app has simple component (see below). makes simple call web api endpoint. can call end point using postman when component tries fails. call never made , no errors output. have done wrong? i've followed pattern before.
import { component, input } '@angular/core'; import { router_directives, router } '@angular/router'; import { http,response, headers, requestoptions } '@angular/http'; import { domsanitizationservice } '@angular/platform-browser'; import { observable } 'rxjs/observable'; import { itile } '../interfaces/tile'; @component({ selector: 'tile', template: require('../tile/tile.component.html'), styles: [require('../tile/tile.component.css').tostring()], directives: [router_directives] }) export class tilecomponent { @input() tile: itile; constructor(private _http: http) { } onselect(id: string) { this.getprofileimage(id); } getprofileimage(id: string): observable<string> { let url = 'http://localhost:2116/api/profileimage/5/'; let headers = new headers({ 'accept': 'application/json' }); let options = new requestoptions({ headers: headers }); return this._http.get(url, options) .map((response: response) => <string>response.json()) .catch(this.handleerror); } private handleerror(error: response) { console.log(error); return observable.throw(error.json().error || 'server error'); } }
the end point looks this...
[allowanonymous] [route("api/profileimage/{userid}")] public httpresponsemessage get(string userid) { return request.createresponse(httpstatuscode.ok, "success"); }
you need subscribe method (a cold observable) fire call.
Comments
Post a Comment