Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4: "Http failure response for (unknown URL): 0 Unknown Error"

I am trying to consume an API URL. I am calling the API with the below code.

import {HttpClient, HttpClientModule, HttpParams} from "@angular/common/http";

@Injectable()
export class PropertyPrefService {

    constructor(private http: HttpClient,
                private configurationService:Configuration) {}

    public searchProjects(propFilter:string):any{
        let temp:any;
        const options = propFilter ?
        {
            params: new HttpParams().set('query', propFilter)
        } : {};

        return this.http.get<any>(this.configurationService.propertySystemApiUrl, options)
                    .subscribe((results:any) => {
                        console.log(results);
                    });
    }

In the Angular code I am not getting any response and am getting an error:

Http failure response for (unknown url): 0 Unknown Error".

However, when I make a request if I open up developer tools on Chrome, I see that the response is received from the server.

The URL is the "https://..." URL and not "http://...".

like image 538
user8300647 Avatar asked Feb 01 '18 07:02

user8300647


4 Answers

The problem is Angular Universal used Express, and the security validates the SSL certificate to the server; I used a self-signed SSL certificate.

The solution is to add NODE_TLS_REJECT_UNAUTHORIZED=0 to your environment to disable the SSL verification in Node.js. You should only set this in development; in production it is very risky.

like image 144
Jose Manuel Casani Guerra Avatar answered Oct 06 '22 14:10

Jose Manuel Casani Guerra


Quoting Sander Elias on Google Groups:

"Error 0 is what you get when the request does not go out. Most common cause of this is that CORS is not configured correctly on the server. Let me rephrase that to: In 98% of the cases, this is a server side issue."

like image 45
Stephan Rauh Avatar answered Oct 06 '22 16:10

Stephan Rauh


You are not in the same domain, same protocol, same port between backend and frontend.

like image 2
guest Avatar answered Oct 06 '22 14:10

guest


Just in case anyone else stumbles on this, none of these solutions worked for me. What worked for me was turning off uBlock Origin. It was blocking any url that had the word "ad" in it for obvious reasons.

like image 2
ymerej Avatar answered Oct 06 '22 14:10

ymerej