2017-10-16 78 views
-1

嘿我試圖做一個離子郵政http服務,但我有很多問題。請有人給我一個離子後http服務與json的基本示例。離子http郵政服務不起作用

以下是我的home.ts代碼請檢查它。

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: "[email protected]", 
      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()); 
    }); 
} 


} 
+0

這是非常相似https://stackoverflow.com/questions/46766284/how-to-make-a-post-and-get-works- in-a-mvc-project/46766561#46766561 –

+0

你應該分享你試過的東西,這樣我們可以指導你。 –

回答

0

你可以試試這個方法

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); 
      }); 
    }); 
    }