Angular4 http service package uses rxjs

http.base.ts

The first step is to introduce rxjs

    import "rxjs/add/operator/toPromise";
    import { RequestOptions ,Headers ,Http,Response ,URLSearchParams } from "@angular/http";

The second step is to create a constructor

    constructor(private http:Http){
        
    }

The third step encapsulates the post request

    public post(url,data,headerType){
    console.log(data);
    let headers = new Headers();//构建一个header头的对象
    console.log(headers);
    if(headerType == "json"){
      headers.append("Content-type","application/json"); //在headers头里面增加一个对象
    }
    if(headerType == "urlencoded"){
      headers.append("Content-type","application/x-www-form-urlencoded");
    }
    if(headerType == "formdata"){
      headers.append("Content-type","multipart/form-data")
    }
    console.log(headers);
    return this.http.post(url,data,{headers:headers,withCredentials:true}).map((res:Response)=>{
      return res.json()//可以在这里面做错误信息的处理
        
    }).catch(err =>{
      throw console.log(err)
    })

}

The fourth step is to introduce this function orderManage.component.ts in the business operation

import {HttpInterceptorService } from "../base/network/http.base";
private getHttp(){
this.HttpInterceptorService.post("https://twms.gxcards.com/app/cmsUser/channelStatus",{},"json").subscribe((res:Response)=>{
  console.log(res)
})

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324433072&siteId=291194637