美文网首页
angular http 封装

angular http 封装

作者: 陈小爬_ | 来源:发表于2018-08-01 17:18 被阅读0次
    /**
     * url带参数带数据库查询(Token)
     * @param url 请求的url
     */
    public getData(url: string): Promise<Object> {
        const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
        headers.set('Authorization', 'Bearer ' + this.token);
        return this.http.get(url, { headers: headers }).toPromise().then((response) => {
            return response;
        });
    }

    /**
     * 提交(插入)数据库数据(Token)
     * @param url 请求的url
     * @param params 参数,键值对象
     */
    public postData(url: string, params: any): Promise<Object> {
        const headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token });

        return this.http.post(url, JSON.stringify(params), { headers: headers, responseType: 'json' })
            .toPromise()
            .then(response => response);
    }

    public postTextData(url: string, params: any): Promise<Response> {
      const headers = new Headers({ 'Content-Type': 'text/plain' });
      headers.set('Authorization', 'Bearer ' + this.token);
      return this.http.post(url, params, { headers: headers })
        .toPromise()
        .then(response => response);
     }

    /**
     * 更新数库数据(Token)
     * @param url 请求的url
     * @param params 参数,键值对象
     */
    public patchData(url: string, params: any): Promise<object> {
        const headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token });
        return this.http.patch(url, JSON.stringify(params), { headers: headers })
            .toPromise()
            .then(response => response);
    }

    /**
     * 删除数据库数据(Token)
     * @param url 请求的url
     */
    public deleteData(url: string): Promise<Object> {
        const headers = new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.token });
        return this.http.delete(url, {
            headers: headers
        })
            .toPromise()
            .then(response => response);
    }

  /**调用*/
    this.ds.postTextData(this.ds.baseApiUrl + 'GetAxClassStc',
        {
           pac: pac,
           basin: basin,
           river: river
        }).then(resp => {
           let da = resp.json().data;
           this.axClass = da;
        }).catch(err => {
            ...
        )

相关文章

网友评论

      本文标题:angular http 封装

      本文链接:https://www.haomeiwen.com/subject/icmrvftx.html