ionic请求php,ionic简单封装http请求

1、ionic g service httpService

2、注册事件

//app.module.ts

import { HttpClientModule} from '@angular/common/http'

@NgModule({

imports:[HttpClientModule]

})

3、在httpService.ts中写入请求逻辑

import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'

import { Storage } from "@ionic/storage";

import { appRestful } from '../appResfuls'

@Injectable({

providedIn: 'root'

})

export class HttpserviceService {

public token = ''

public httpHeader: any = ''

public commonUrl = ''

constructor(

public http: HttpClient,

public storage: Storage,

) {

this.commonUrl = appRestful.getProdUrl()

}

//封装get请求 需要两个参数

async httpGet(url, params) {

let commonUrl = this.commonUrl

let httpOption

await this.storage.get('authentication').then(val => {

if (val) {

httpOption = {

headers: new HttpHeaders({

'authentication': val

}),

params: params

}

} else {

httpOption = {

params: params

}

}

})

return await new Promise((resolve, reject) => {

this.http.get(commonUrl + url, httpOption).subscribe((res) => {

resolve(res)

}, (err) => {

reject(err)

})

})

}

//POTS请求 需要三个参数

async httpPost(url, params) {

let httpOption

await this.storage.get('authentication').then(val => {

httpOption = {

headers: new HttpHeaders({

'authentication': val

})

}

})

return await new Promise((resolve, reject) => {

this.http.post(this.commonUrl + url, params, httpOption).subscribe((res) => {

resolve(res)

}, (err) => {

reject(err)

})

})

}

//PUT请求

//DELETE请求

}

4、appResful.ts中放接口

export class appRestful{

//定义接口地址

public static interfaces={

login:'/v4/authentications', //登录

}

//测试环境URL

public static getDevUrl(){

return 'http://192.168.1.171:9090'

}

//生产环境URL

public static getProdUrl(){

return 'http://prd9.hbjk.com.cn:8080'

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值