首页 > 学院 > 开发设计 > 正文

ionic 联网工具 HttpHelper

2019-11-08 00:24:22
字体:
来源:转载
供稿:网友
import { Injectable } from "@angular/core";import { Http, Headers } from '@angular/http';import 'rxjs/add/Operator/timeout';import 'rxjs/add/operator/map';import 'rxjs/add/operator/toPRomise';@Injectable()export class HttpHelper {    static GET(http: Http, options) {        let url = options.url;        let params = options.data;        let success = options.success;        let error = options.error;        url = url + "?jsoncors=jsoncors&" + HttpHelper.JsonString(params);        return http.get(url).timeout(3000).toPromise()            .then(response => {                if (success != null && success != undefined) {                    success(response);                }            })            .catch(() => {                if (error != null && error != undefined) {                    error();                }            });    }    static POST(http: Http, options) {        let url = options.url;        let params = options.data;        let success = options.success;        let error = options.error;        params = "jsoncors=jsoncors&" + HttpHelper.JsonString(params);        let headers = new Headers();        headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');        return http.post(url, params, { headers: headers }).timeout(3000).toPromise()            .then(response => {                if (success != null && success != undefined) {                    success(response);                }            })            .catch(() => {                if (error != null && error != undefined) {                    error();                }            });    }    static JsonString(json) {        let params = [];        for (let o in json) {            if (json.hasOwnProperty(o)) {                params.push(o + '=' + json[o]);            }        }        return params.join('&');    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表