javascript - Angular http post to return data back through service to component -
i want return data (@@identity) sql server through service component
the web api being called , sending data back.
however, since new angular 2/4 ( used angular 1 ) return
on service , , set variable or object caller.
currently doing
component
this.trackerformservice.addsession(); // current
but since value single id, 5, shouldn't create in typescript retrieve it?
this.myid = this.trackerformservice.addsession(); // ???
then in service
private _url = 'http://localhost:60696/api/tracker'; addsession() { let headers = new headers({ 'content-type': 'application/json' }); let options = new requestoptions({ headers: headers }); this.http.post(this._url, json.stringify({}), options).catch(this.handleerror).subscribe(); }
now above pass id service (addsession() method component, shouldn't return
?
return this.http.post(this._url, json.stringify({}), options).catch(this.handleerror).subscribe();
but work?
web api (.net core ) looks
return created($"api/sessiontrackers/{sessiontracker.id}", sessiontracker);
this surely making post call web api
in service
return this.http.post(url, json.stringify(data), requestoptions) .map((response: response) => response.json())
in component
this.service.addsession(this.data).subscribe( data => { console.log(data) // data returned call }, error => { console.log(error); // error if }, ()=> // here call completed. if wish // after call completed(since asynchronous call), right place do. ex: call function )
Comments
Post a Comment