angular - Http service POST -


i have data (http service) in application on angular 2:

    private _url = "http://10.77.7.77/monitoring/api/v1/services/list";      constructor(private _http: http) {      }      getservices(): observable<any> {         return this._http.get(this._url)             .map(res => res.json());     } 

and ok, task create post data, url:

private _url2 = 'http://10.77.7.77/monitoring/api/v1/action/stop'; 

id - number of service json:

[{ "nodename": "10.46.2.152", "servicelist": [{     "id": "3827aa4b204fc2e122f452c1c1ceeaf15109364698d5d0f2153efa6e9487a968",     "name": "nginx",     "status": "online",     "servicecontrolled": true }] 

i wrote post data:

postservices(post: { title: string, status: string, id: number }): observable<any> {         let _url3 = `${this._url2}/${post.id}`         let headers = new headers();         headers.append('content-type', 'application/json');         const body = json.stringify(post);         return this._http.post(_url3, body, post)             .map(res => res.json())     } 

with event

onpost(title: string, body: string, id: string) {   this._service.postservices({title: title, body: body, id: +id})     .subscribe(       response => this.response = response,       error => console.log(error)     ); } 

and template:

<button *ngif="!!service_rec.servicecontrolled"  [style.background-color]="service_rec.controlled   == 'true' ? 'green' : 'orange'"  class="btn btn-warning"  (click)="onpost()">  {{ service_rec.servicecontrolled | json | toonoff }}  </button> 

and have error 500 (internal server error)

if want post json data, use following:

// rc2 postservices(post: { title: string, body: string, id: number }): observable<any> {     return this._http.post(this._url2, post)         .map(res => res.json()) } 

angular2 set automatically application/json content type , serialize post object (json.stringify).

if want serialize own json content (before rc2 excluded), need add content type explicitly:

postservices(post: { title: string, body: string, id: number }): observable<any> {     let headers = new headers();     headers.append('content-type', 'application/json');     return this._http.post(this._url2, post, { headers })         .map(res => res.json()); } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -