I am using Ionic 2 Beta 11. I am trying to send a request to an external API that will return a JSON response. I have overcome CORS problems and I am communicating with the API, but I cannot send body information that is required. I tried to format the body variable in the following ways, each of which was unsuccessful:
As an object: {email: this.email, password: this.password}
As a string object: JSON.stringify({email: this.email, password: this.password})
Like a line: 'email=' + this.email + '&password=' + this.password
Here is my code:
import {Component} from '@angular/core'; import {NavController, MenuController} from 'ionic-angular'; import {Http, Headers, RequestOptions} from '@angular/http'; import 'rxjs/Rx'; @Component({ templateUrl: 'build/pages/login/login.html' }) export class LoginPage { nav: NavController; username: string; password: string; constructor(nav: NavController, private http: Http) { this.nav = nav; } doLogin() { let body = JSON.stringify({ email: this.username, password: this.password }); let headers = new Headers({ 'NDAPI-Key': 'XXXXXXXXXX', 'NDAPI-Host': 'XXXXXXXXXXX' }); let options = new RequestOptions({ headers: headers }); this.http .post('/api', body, options) .map(res => res.json()) .subscribe( data => { console.log(data); }, err => { console.log("ERROR!: ", err); } ); } }
source share