In Angular 16, instead of receiving the actual error message, I am faced with an unexpected error response "Http failure response for http://localhost:8080/api/tutorials: 0 Unknown Error"

Currently, I am utilizing Angular 16 HttpClient to make requests to external services using a standard configuration:

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Tutorial } from '../models/tutorial.model';
import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
const baseUrl = 'http://localhost:8080/api/tutorials';
@Injectable({
  providedIn: 'root',
})
export class TutorialService {
  headers = new HttpHeaders().set('Content-Type', 'application/json');
  httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };


  constructor(private http: HttpClient) { }
  handleError(error: HttpErrorResponse): Observable<never> {
    console.error('An error occurred:', error);
    // You can throw an error or return an observable here
    return throwError('Something went wrong'); // Example of returning an observable with an error message
  }
  getAll(): Observable<Tutorial[]> {
    return this.http.get<Tutorial[]>(baseUrl, this.httpOptions).pipe(catchError((e: any) => this.handleError(e)));
  }

  get(id: any): Observable<Tutorial> {
    return this.http.get<Tutorial>(`${baseUrl}/${id}`, { headers: this.headers });
  }

The issue arises when the request fails and I receive a vague Http failure response for http://localhost:8080/api/tutorials: 0 Unknown Error message in the console. https://i.sstatic.net/Fa3Ce.png

Answer №1

The issue was replicated by me when I utilized your service class again. The reason for the error in my situation is due to the absence of a server listening on localhost:8080.

https://i.sstatic.net/KkWyI.png

This is likely the main cause, so it would be wise to investigate this first.

To confirm, you can use a separate HTTP client such as cURL to check your server:

curl localhost:8080

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to assign the selected attribute to a dynamically loaded Ion-select component in Ionic 2

I'm facing an issue with dynamically loading <ion-select> and setting default selection using the selected attribute. It doesn't seem to work as expected. Can anyone help me understand why? You can view the code on Plunker app/home.page.h ...

Is the lazy loading feature for nested modules in an Angular Ionic4 application experiencing functionality issues?

My application includes both public routes like login and register, as well as protected routes for members. The member routes contain child routes within tabs, with additional child routes for categories, items, and more. However, when running 'ionic ...

Blank webpage caused by ngx TranslateService

Every time I attempt to translate Angular components using ngx-translate/core by utilizing the translateService in the constructor, the page appears blank. This is my appModule : import { NgModule } from '@angular/core'; import { BrowserModule } ...

It appears that Spring Boot is not making a custom header visible to my frontend application

Currently, I am working with a Spring Boot backend and an Angular frontend, where my goal is to enable the download functionality for a pdf file. To achieve this, I have included the following handler in my REST-controller: @GetMapping("/{id}" ...

Master the art of properly switching on reducer-style payloads in Typescript

Currently, I am dealing with two types of data: GenArtWorkerMsg and VehicleWorkerMsg. Despite having a unique type property on the payload, my Searcher is unable to differentiate between these data-sets when passed in. How can I make it understand and dis ...

The Angular Material Calendar is always designed to highlight the current date and keep focus on today's day

When I click on a date in the Angular Material Calendar, the tile for today's date is always highlighted. This occurs even if the selected date is in a different month. Do you think this behavior is a bug or a feature? I personally believe it is a fe ...

OpenTok Angular 6 encountered an error with code TS2314 stating that the generic type 'Promise<T>' needs to have 1 type argument specified

Issue in opentok.d.ts File: Error TS2314 npm version: 6.2.0 node: v8.10.0 Angular CLI: 6.2.3 Operating System: Linux x64 Angular Version: 7.0.0-beta.5 @opentok/client": "^2.14.8 ...

Angular does not display HTML content until the requested http data has been fully loaded

I am experiencing an issue where certain HTML content does not load until the component has received data from an API call through a service. Below is the relevant code snippet: import { ApiService } from './services/api.service'; @Component({ ...

Resizing svg to accommodate a circle shape

As I work on my vue.js app that involves a plethora of diverse icons, I made the decision to create a small icons builder in node.js. The purpose is to standardize their usage and also "crop" each SVG so it fits perfectly within its parent container by uti ...

Listening to changes in a URL using JQuery

Is there a way to detect when the browser URL has been modified? I am facing the following situation: On my webpage, I have an iframe that changes its content and updates the browser's URL through JavaScript when a user interacts with it. However, no ...

Displaying svg files conditionally in a react native application

I have developed an app specifically for trading dogs. Each dog breed in my app is associated with its own unique svg file, which are all stored in the assets folder (approximately 150 svg files in total). When retrieving post data from the backend, I re ...

Tips for successfully mocking axios.get in Jest and passing AxiosPromise type value

I attempted to simulate the axios.get() function using the code below, however TypeScript is returning an error stating "argument of type '{ data: expectedResult }' is not assignable to parameter of type 'AxiosPromise<{}>'". Can ...

Unable to connect my Angular library with another Angular library

Issue with Angular Libraries Within my project, I have created several angular libraries stored in different folders: /home/user/lib1 /home/user/lib2 /home/user/lib3 The lib2 and lib3 libraries are dependent on the lib1 library. // Example of the lib3.m ...

What is the best way to generate conditional test scenarios with Protractor for testing?

Currently, there are certain test cases that I need to run only under specific conditions. it ('user can successfully log in', function() { if(siteAllowsLogin) { ..... } The problem with the above approach is that even when sitesNo ...

Displaying the count of items outside the Angular 2 ngFor loop

As a beginner in Angular, I've encountered a small issue. How can I display the actual number of items outside of an *ngFor loop? Currently, I'm using filter and pagination pipes in this manner: *ngFor="let item of items | filterBy: ['name ...

Arrange in chronological order and organize based on an external array

Presenting an Array of Objects with various transactions: [{ date: 2022-04-07T01:00:00.000+00:00, type: 'profit' amount: 200 },{ date: 2022-04-07T01:00:00.000+00:00, type: 'withdraw' amount: 600 },{ date: 2022-04-07T01:00: ...

Troubleshooting: Empty Rows displayed in PrimeNG Table

Experimenting with Angular 8 and the primeNG package, I'm facing an issue where I cannot retrieve values. Despite using the {{staff[col.field]}} syntax, I only get empty rows of data. However, when I utilize the interface definition like {{staff.Emplo ...

Form with checkboxes in a Next.js and Typescript application

I am currently working on a project using Next.js and Typescript. I have implemented a form in this project, which is my first experience with Typescript and checkbox types. However, I am encountering difficulties in retrieving all checkbox values, adding ...

Default functionality of Typescript paths imports fails to operate properly

Can anyone help me figure out how to set up default imports in my .ts files using the paths specified in my tsconfig.base.json? I have this file defined as default in the File Type > Typescript Config. https://i.sstatic.net/LvBGV.png The import statem ...

Triggering an event within a component to execute a specific function in another component

I am working on a component that includes multiple routes such as home, app, and navbar. My goal is to have the encrementcalc() function execute when the navbar button is pressed. I attempted to use the event emitter but was unsuccessful. Can someone prov ...