Angular Missing Http Error Status

I am using Http Client which is an enhanced version from Angular 4 Http Client

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

      

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

I have an implementation like this:

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);
            }
        });
    }

      

Quite similar to Angular recommends on their page ( https://angular.io/docs/ts/latest/guide/server-communication.html )

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 can't get the status code at all.

Why is this happening? I must be extending Httpclient and I did something wrong? Even I get this weird status: 0, the console also shows in 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 a good CORS setup, if the token hasn't expired yet, I get the info successfully)

+3


source to share


2 answers


First, you can double check the CORS configuration on the server. I know, I know, you said you had it well tuned and I believe you because I went through this rigamarole myself. But the CORS response is completely server-specific and shouldn't have anything to do with your AngularJS. I've personally seen Django installations where each response method had its own CORS configuration, so it's possible that an unauthorized request isn't getting anything, including permissions even to make a Cross Origin request.

Secondly, regarding the error code, my first thought is that the server side code that generates the response needs to be revised. I've never seen Angular return a response code that wasn't directly from the server (which, I haven't seen everything). My guess is that maybe there is a default status code or a misconfigured variable returned from the server.



tl; dr: It seems that both problems, CORS

and status: 0

are a bug of something on the server.

+3


source


Two days of debugging and in my case the answer was adding a backslash to the end of the url.

website.com/api/create_user

      

Does not work...



website.com/api/create_user/

      

We worked !!!!

0


source







All Articles