Angular 4: "Http error failed for (unknown URL): 0 Unknown error"

I am trying to use the API url. I am calling the API with the code below.

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 Angular code, I don't get any response and get an error message:

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

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

The URL is "https: // ...", not "http: // ...".

+17
source share
8 answers

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

The solution is to add NODE_TLS_REJECT_UNAUTHORIZED=0 to your environment to disable SSL checking in Node.js. You should install this only during the development process; in production is very risky.

+5
source

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

+2
source

Agree with @StephanRauh and want to add a few points to yours . This happens when the request is not sent or for some reason cannot contact the server .
The reasons may be:

  1. If the Internet connection is very slow to connect to the server or not available at all.
  2. The server itself is not working, i.e. There is no handler for our request.
+2
source

Quote Sander Elias on Google Groups :

"Error 0 is what you get when a request is not sent. The most common reason for this is because CORS is not configured correctly on the server. Let me rephrase this as follows: In 98% of cases, this is a server-side problem."

+1
source

In case anyone else stumbles upon this, none of these solutions worked for me. Disabling uBlock Origin worked for me. It blocked any URL that had the word "advertisement" for obvious reasons.

+1
source

I had exactly the same problem. I was able to see the result in the Chrome developer tools. But I always got the error message Http failure response for (unknown url): 0 Unknown Error .

In my case, I used responseType:'text' to request the text/html payload. But for some reason this is not compatible. I had to switch to responseType: 'blob' and use FileReader to convert blob to string.

So, I think you have the same problem when the returned content does not match the configured responseType . By the way, the default is json .

  this.httpClient.get(url, {responseType: 'blob'}) .subscribe(data => { const reader = new FileReader(); reader.onloadend = (e) => { const readerResult = reader.result; console.log(readerResult); // Print blob content as text }; reader.readAsText(data, 'UTF-8'); }, error => console.log(error)); 
0
source

I ran into this problem when testing angular applications from iPhone / iPad devices. The application worked fine from the desktop. I have enabled Cors on the host server, but still getting this error. So, I did the following to solve this.

Run ng serve using IP: ng serve --host 192.168.1.10

Point APIs using IP: In environment.ts, I changed the URL below:

 apiUri: 'http://192.168.1.10:9000/api' 

Now I can access the URL from mobile devices.

0
source

TL DR: /etc/hosts missing an entry with a domain name.

In Angular Universal, I had a problem related to the fact that part of the SSR throws this error due to server-side rendering (;)).

Say my site was available at https://myproject.com/ and it works well. When you directly click on any URL, the SSR starts. Now, if the SSR needs to make an API call, it will make an HTTP request to https://myproject.com/api/blah/show on the local network. And it threw a mistake somewhere.

For debugging, I tried running curl https://myproject.com/api/blah/show (on VPS) and got an error.

To fix this, I needed to add the following line to my /etc/hosts file:

 127.0.0.1 myproject.com 

After that, both the curl command and the SSR worked!

Stack:

  • Ubuntu 16 & 18
  • Nginx
  • Let SSL Encrypt
  • Angular station wagon
  • express
0
source

Source: https://habr.com/ru/post/1275096/


All Articles