3

I am learning to call api in ionic for sign in purpose. and I don't know the proper way to call it.

I have tried to call it but it did not success!

signin.ts

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { HTTP } from '@ionic-native/http'

@Component({
  selector: 'page-sign-in',
  templateUrl: 'sign-in.html',
})
export class SignInPage {

  private signinUrl:"*someurl*";

  email="";
  password="";

  constructor(public navCtrl: NavController, public navParams: NavParams,
  private http:HTTP) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SignInPage');
  }

  signinClick(){
    this.callSignInApi(this.email,this.password);
  }

  callSignInApi(email:string,password:string){
    console.log('api email',email);
    console.log('api pass',password);

    let urlSearchParams = new URLSearchParams();
    urlSearchParams.append('username', email);
    urlSearchParams.append('password', password);

    this.http.post(this.signinUrl,urlSearchParams.toString(),{})
    .then(data => {

      console.log("data status",data.status);
      console.log("data data",data.data); // data received by server
      console.log("data headers",data.headers);

    })
    .catch(error => {

      console.error("catching error",error);
      console.log("error status",error.status);
      console.log("error error",error.error); // error message as string
      console.log("error headers",error.headers);

    });
  }

}

Everytime when I click on the button to call the api, It gives me error (but, error is also null). Even I don't know what the error is.
Can anyone guide me how to call api in proper way, and how to pass the parameters in proper way.

3
  • 1
    are you using http from import { Http } from '@angular/http'; Commented May 14, 2018 at 7:37
  • 1
    It is always worth taking a look in the network tab of your developer tools to see the actual request being made. I suspect that urlSearchParams.toString() is not doing what you need. Commented May 14, 2018 at 7:37
  • you can solve using different way but standard way you need to create service for http request handling Commented May 17, 2018 at 12:11

4 Answers 4

1

You can use Http from @angular/http and make a proper api call.

First add to app.module.ts:

import { HttpModule } from '@angular/http'; imports: [ .., HttpModule,

add your page:

inject http: constructor(public http: Http,.....

api call:

this.http.post(this.signinUrl,urlSearchParams.toString(), {}).subscribe( data=>{ ...... },error=>{ ..... });

Sign up to request clarification or add additional context in comments.

1 Comment

Same url I am using in my another android app. And it is working fine there.
1

Try this :

let urlSearchParams  = {
      'username': email,
      'password': password
  };

this.http.post(this.signinUrl,urlSearchParams,{})
    .then(data => {
         // data received by server
     })
     .catch(error => {
         // error message as string
     });

hope this will work!, if not try using headers like

this.http.post(this.signinUrl,urlSearchParams,{'Content-Type': 'application/x-www-form-urlencoded'})
.then(data => {
  // data received by server
})
.catch(error => {
  // error message as string
});

1 Comment

Still getting same results! getting error. (but the object of error is also null).
1

Try to convert parameter object to URI and then pass like below my working example

this.http.post(this.signinUrl,this.formdata(urlSearchParams),{'Content-Type': 'application/x-www-form-urlencoded'})
.then(data => {
 // data received by server
})
.catch(error => {
// error message as string
});



/** Convert Object to parameter to send in http request (For not sending json object) */
 formData(myFormData) {
   return Object.keys(myFormData).map(function (key) {
     return encodeURIComponent(key) + '=' + encodeURIComponent(myFormData[key]);
 }).join('&');
}

Comments

0

Import from Angular's HTTP package.

import { Http } from '@angular/http';

and remove toString() in case of HTTP POST request:

this.http.post(this.signinUrl,urlSearchParams).subscribe(
    data=>{
    ......
   },error=>{
    .....
   });

5 Comments

I've replaced import statement. and chaged code as you told, but now i am getting runtime error that StaticInjectorError(AppModule)[SignInPage->Http]: NullInjectorError: No provider for Http!
You might not be having HttpModule in your AppModule ts file. Use import { HttpModule } from '@angular/http'; and add in imports section.
After adding this, I am getting error status 404 when I click sign in button to call api
U should parse object into string JSON.stringify(urlSearchParams); in post method
@Sanoj_V using JSON.stringify(urlSearchParams); did not work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.