简体   繁体   中英

Ionic http post service is not working

Hey I m trying to make a post http service in ionic, but i have many problems. Please someone give me a basic example of a ionic post http services With json.

following is my home.ts code please check it.

  import { Component } from '@angular/core'
    import { NavController } from 'ionic-angular';
    import { Http, Headers, RequestOptions } from '@angular/http';
    import 'rxjs/add/operator/map';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  constructor(public navCtrl: NavController, public http:Http) {

  }
getit(){
    console.log("kfdsf");
    let headers = new Headers();
    headers.append('content-type', 'application/json');

    let options= new RequestOptions({headers: headers});



    var data = {
      "login":{
            username: "inspector@epde",
            password: "Passwor23"
        }
  }

    this.http.post('url',JSON.stringify(data),options).map(res=>res.json()).subscribe(data=>{
      console.log(data)
    }, err=>{
      console.log("Error!:", err.json());
    });
 }


}

You can try this way

login(credentials) {
    return new Promise((resolve, reject) => {
        let headers = new Headers();
        headers.append('Content-Type', 'application/json');

       this.http.post(apiUrl+'login', JSON.stringify(credentials), {headers: headers, withCredentials: true})
          .subscribe(res => {
            resolve(res.json());
          }, (err) => {
            reject(err);
          });
    });
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM