IONIC 2 - 从服务中返回虚拟数据

时间:2017-02-09 09:11:58

标签: angular ionic2 angular2-http

我正在使用以下代码检查每个http请求的网络可用性。

get(url) {
    this.createAuthorizationHeader();
    if (this.networkService.noConnection()) {
        this.networkService.showNetworkAlert();
        return Observable.of({ success: false });
    }
    else
        return this.http.get(HttpClient.ApiBaseUrl + url, { headers: this.headers });
}

post(url, data) {
    //return { "success": false };
    if (url !== 'Login')
        this.createAuthorizationHeader();
    if (this.networkService.noConnection()) {
        this.networkService.showNetworkAlert();
        return Observable.of({ success: false });//.delay(1000);
    }
    else
        return this.http.post(HttpClient.ApiBaseUrl + url, data, { headers: this.headers });
}

在我的服务档案中,我处理的是响应&错误。

public extractData(res: Response) {
    let body = res.json();       
    return body || {};
}

public extractError(error: Response | any) {
    // In a real world app, we might use a remote logging infrastructure

    let errMsg: string;
    if (error instanceof Response) {
        errMsg = `${error.status} - ${error.statusText || ''}`;
    } else {
        errMsg = error.message ? error.message : error.toString();
    }
    console.error(errMsg);
   return Observable.throw(errMsg);
}


getClientServiceById(serviceId: any) {
    if (!serviceId)
        serviceId = this.emptyGuid;

    return this.httpClient.post('Service/Detail', { serviceId: serviceId })
        .map(this.extractData)
        .catch(this.extractError);
}

当网络可用时,一切正常。 Bun net断开连接我在地图中收到错误。错误是 res.json不是函数

谁能告诉我如何在地图中传递json对象?

0 个答案:

没有答案