Angular2经过身份验证的Web请求获取错误消息

时间:2016-05-06 10:59:13

标签: angular angular2-http

我正在使用安全的api,要求我在每个请求上发送一个Authorization标头,这很好。但是我对401响应的处理存在问题。

我对API的调用如下:

let headers: Headers = new Headers();            
headers.append('Authorization', 'Bearer ' + {myauthtoken});

let request = this.http.get(Utils.getUrl('/somesecuredmethod'), { headers: headers });
        request.subscribe(
            result => { this.setLoginDetails(result); },
            error => {
                this.loginFailed = true;
                this.reasonLoginFailed = error;
                return true;
            });

在错误中我可以看到响应代码401,但是我也在chrome控制台中看到以下内容

GET https://localhost/somesecuredmethod 401 (User not found in database.)

有谁知道如何以角度显示消息文本?

谢谢,Jon。

编辑:

我之前使用纯ajax调用了同样的方法,如下所示,可以获取错误文本。

$.ajax({
    url: baseurl + "/somesecuredmethod",
    method: "get",
    headers: { 'Authorization': 'Bearer ' + accessToken }
}).done(function (data) {
    // do something with the success
}).fail(function (jqXHR, status, err) {
    $("#loading_application").addClass("hidden");
    $("#login_failed_reason").text(err);        <--- here I can the error text shown in the dev console
    $("#login_failed").removeClass("hidden");
});

1 个答案:

答案 0 :(得分:0)

如果它是有效负载中的文本消息,则可以使用错误的Parse error: syntax error, unexpected T_VARIABLE in .../admin-panel/remove.php on line 5 方法:

text
相关问题