Angular 2 handleError请求方法

时间:2017-05-05 10:50:50

标签: angular

发布请求

post(url, data, params = {}, headers = {}) {
        this.loaderService.show();
        return this.http
            .post(url, data, this.options(params, headers))
            .map(this.transformResponse)
            .catch(this.handleError)
            .finally(() => {
                this.loaderService.hide();
            });
    }

选项方法

private options (params, headers) {
        let _headers = new Headers();
        if (sessionStorage.getItem('auth_token')) {
            _headers.append('Authorization', 'Bearer ' + sessionStorage.getItem('auth_token'));
        }
        _headers.append('Content-Type', 'application/json');
        _headers.append('Accept', 'application/json');

        let _search = new URLSearchParams();
        if (params instanceof URLSearchParams) {
            _search.replaceAll(params);
        }else {
            for (let param in params) {
                _search.set(param, params[param]);
            }
        }

        let options = new RequestOptions({ headers: _headers, search: _search});

        return options;
    }

我的错误处理

private handleError = (err) => {
let res = {
            status: err.status,
            statusText: err.statusText,
            data: {}
        };}

如何在handleError function

中捕获所请求的方法

我得到的当前回应是

response_body: "{↵  "associateToken": "SDFWETSDFSDFW4"↵}"headers: Headers_headers: Map_normalizedNames: Map__proto__: Objectok: falsestatus: 403statusText: "Forbidden"type: 2url: "xyz.com"

如何获取请求方法发布或获取上述响应。任何投入都受到高度赞赏。

0 个答案:

没有答案