Examples of "post(url: string, body: string, options?: RequestOptionsArgs) : Observable<Response>"

Can I get some examples using the post() method of Http in Ionic 2
Suppose I want to send a JSON object containing username and pasword to a file ‘http://127.0.0.1/login/login.php’ and return some JSON object.
Please help me with some examles.

you can find example https://angular.io/docs/ts/latest/guide/server-communication.html

2 Likes

Maybe something like this? Psuedo code:

import "rxjs/Rx"; // required for the .map function
import {Http, Headers} from "angular2/http";

// Prepare object
let object = { username: "myUsername", password: "myPassword"};

// Start the post function and process the response
this.post(object).then(response => {
     console.log(response);
});

// Post function
post: (object: Object) : Promise<JSON> => {       
     let headers = new Headers(); 
     headers.append("Content-Type", 'application/json');  
     return this.http.post("http://127.0.0.1/login/login.php", JSON.stringify(object), { headers: headers  }).map((response) => {                
          return response.json();
     }).toPromise();            
}

EDIT: Added the required imports.

1 Like