Angular Http错误状态丢失 [英] Angular Http Error status missing

查看:70
本文介绍了Angular Http错误状态丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Http客户端,它是Angular 4的Http客户端的扩展版本

I'm using an Http client wich is an extended version from Angular 4's Http Client

export class SecurityClient extends Http {
// ...
}

在此客户端中,我有尝试对api进行调用的方法,并且我想捕获401状态以尝试刷新令牌.

In this client I have methods that attempt calls against an api and I want to catch 401 status to try a refresh token.

我有一个这样的实现:

get(url: string, options?: RequestOptionsArgs): Observable<Response> {
        return super.get(url, this._getOptions(options)).catch((initialError: any) => {
            console.log('error: ' + JSON.stringify(initialError));
            if (initialError && initialError.status === 401) {
                return this.authService.doRefreshTokenObservable().flatMap((ok) => {
                    if (ok) {
                        return super.get(url, this._getOptions(options));
                    } else {
                        return Observable.throw('Authentication error');
                    }
                });
            } else {
                return Observable.throw(initialError);
            }
        });
    }

非常相似,例如angular在其页面中推荐( https://angular.io/docs/ts/latest/guide/server-communication.html )

Pretty much similar like angular recommends in its page (https://angular.io/docs/ts/latest/guide/server-communication.html)

但是由于某种原因,第一个console.log显示如下内容:

But for some reason, the first console.log shows something like:

error: {"_body":{"isTrusted":true},"status":0,"ok":false,"statusText":"","headers":{},"type":3,"url":null}

我根本无法获得状态码.

And I'm unable to get status code at all.

为什么会这样?这是由于我正在扩展Httpclient而我做错了什么吗?即使我的状态为0,控制台也会显示红色字母:
"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401." (我们已正确配置了CORS,如果令牌尚未过期,我会成功检索信息)

Why is it happening? It's due I'm extending Httpclient and I did something wrong? Even I'm getting this weird status:0, console also shows with red letters:
"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401." (we have the CORS well configured, if token has not expired I retrieve the information successfully)

推荐答案

首先,您可能需要仔细检查服务器上的CORS配置.我知道,我知道,您说您已经对其进行了正确配置,并且我相信您,因为我本人已经经历了这个rigamarole.但是,CORS响应完全来自服务器,与AngularJS无关.我亲自看过Django设置,其中每个响应方法都具有自己的CORS配置,因此未经身份验证的请求有可能不被给予任何东西,甚至包括发出跨源请求的权限.

First, you may want to double check your CORS config on the server. I know, I know, you said you've got it well configured, and I believe you because I've gone through this rigamarole myself. But the CORS response is entirely from the server and shouldn't have anything to do with your AngularJS. I've personally seen Django setups where each response method had it's own CORS configuration, so it's possible that an unauthenticated request is given nothing, including permissions to even make the Cross Origin Request.

第二,关于错误代码,我首先想到的是应该检查正在生成响应的服务器端代码.我从未见过Angular返回不是直接来自服务器的响应代码(嘿,我还没有看到一切).我的假设是,可能是默认状态代码,或者是从服务器返回的配置错误的变量.

Second, as far as the error code, my first thought is that the server side code that is generating the response should be reviewed. I've never seen Angular return a response code that wasn't straight from the server (which hey, I haven't seen everything). My assumption is that there is perhaps a default status code, or a misconfigured variable that is being returned from the server.

tl; dr:似乎两个问题,CORSstatus: 0都是服务器上的故障.

tl;dr: It seems like both problems, CORS and status: 0 are the fault of something on the server.

这篇关于Angular Http错误状态丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆