嘿我试图在离子中发布一个http服务,但我有很多问题。请有人给我一个离子帖子http服务的基本例子用json。
以下是我的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: "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());
});
}
}
答案 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);
});
});
}