1.get请求
1.创建服务,并引入
import{Injectable}from'@angular/core';
import'rxjs/add/operator/map';
@Injectable()
exportclassCategoryListServiceProvider{
}
publicgetList(params) {
console.log(params);
returnnewPromise((resolve,reject)=>{
.map(res=>res.json())
.subscribe(data=>{
resolve(data);
},err=>{
reject(err);
})
})
}
}
2.在需要调用的ts文件引入以下服务和方法
import{Component}from'@angular/core';
import{IonicPage,NavController,NavParams}from'ionic-angular';
import{CategoryListServiceProvider}from'./../../../providers/category-list-service/category-list-service';
3调用服务
exportclassCategoryListPage{
constructor(publicnavCtrl:NavController,publicnavParams:NavParams,publicservice:CategoryListServiceProvider) {
letgid=navParams.data;
letparams:URLSearchParams=newURLSearchParams();
params.set('city_id','1');//城市ID
params.set('lat','34.2351921');
params.set('lng','108.9421249');
params.set('p','1');
params.set('order','1');
params.set('group_id',gid);
this.service.getList(params).then(
data=>{
letdataString=JSON.stringify(data);
letdataJson=JSON.parse(dataString);
console.log(dataJson)
})
}
ionViewDidLoad() {
console.log('ionViewDidLoad CategoryListPage');
}
}
2.解决post请求问题
1.服务和get不同
2.使用先引入以下
3.使用headers请求头,可代参