didinj / ionic2-rest-authentication

Ionic 2 REST API authentication
MIT License
19 stars 14 forks source link

Sending json values to service #4

Open ramukodandapuram opened 7 years ago

ramukodandapuram commented 7 years ago

I was not able to sending values to rest api. I don't know what I done wrong. help me get rid of this!

here is my code.

import { Injectable } from '@angular/core'; import { Http, Headers, RequestOptions } from '@angular/http'; import 'rxjs/add/operator/toPromise';

@Injectable() export class AuthServiceProvider {

http: any;
baseUrl: string;

constructor(http: Http) { //console.log('Hello AuthServiceProvider Provider'); this.http = http; this.baseUrl = 'http://www.60.207.162.74:8080/api/WebApi/Login';

} login(username, password) {

  return new Promise((resolve, reject) => {
      let body = JSON.stringify({ "LoginName": username, "Password": password });
      let headers = new Headers();

      headers.append('Content-Type', 'application/json');
      let options = new RequestOptions({
          headers: headers
      });

  return this.http.post(this.baseUrl, body, options)
      .subscribe(res => {
          resolve(res.json());
         alert("Success");  //if everything go well

      },

      (err) => {
          reject(err);

      });
  });

}

}

ramukodandapuram commented 7 years ago

yea..I got the solution!!. here main problem with url. It' working fine for Phonegap but not for Ionic, Im still wondering!