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


#1

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.


How do I send a JSON (serialized if possible ) through http POST method
#2

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


#3

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.