Issue with Angular: HttpClient unable to retrieve local JSON file

I am using the newest HttpClient module in Angular5 to send a get request for local json, but I keep receiving a 404 error. I have already imported the HttpClientModule into my app.module.ts.

Here is a snapshot of my folder structure:

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

I am trying to make the call from the blog-detail component for the blog-list.json. Below is the code snippet:

constructor(private http: HttpClient) {
  }

  ngOnInit(): void {
    // this.http.get('https://jsonplaceholder.typicode.com/posts/1').subscribe((data:Blog[]) => {
    this.http.get('app/api/blog-list.json').subscribe((data:Blog[]) => {
         this.blogs = data;
    });
  }

The error message I receive is as follows:

GET http://localhost:909/app/api/blog-list.json 404 (Not Found)

When attempting to open the file directly in the browser by clicking http://localhost:909/src/app/api/blog-list.json, the application itself is served rather than the intended json file.

Kindly note that while requests to a REST API on the web are successful, calls solely to a local json file seem to fail.

Answer №1

After some troubleshooting, I managed to resolve the issue that occurred in the first place.

To fix it, I made changes to the .angular-cli.json file by including the path to the api folder in the assets property. The updated assets property now appears as:

"assets": [
    "assets",
    "app/api",
    "favicon.ico"
 ]

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

incorporating my unique typographic styles into the MUI framework

I'm currently working on customizing the typography for my TypeScript Next.js project. Unfortunately, I am facing difficulties in configuring my code properly, which is causing it to not work as expected. Can someone kindly provide assistance or guida ...

Error: The value associated with the key 'services.authentication.basic.user.service' is not defined in the current context, causing a ResolutionError with a code of

I have been working on creating custom authentication using LoopBack 4. I referred to the following documentation: services.authentication.basic.user.service is showing as not bound. This pertains to the user.controller where I've injected JWTAuthen ...

PrimeNG's Angular component pTree TreeNode

Is there a way in Angular to retrieve the positions of nodes within a TreeNode hierarchy based on their display order? I have data structured as TreeNode objects, which include children that can branch off further. How can I access these nodes according t ...

Angular: Troubleshooting blank pages in HTML - What's causing it?

This is the content of my heroes component: <!--The content below is only a placeholder and can be replaced.--> <div style="text-align:center"> <h1> <h2>{{hero.name}} Details</h2> <div><s ...

How to transform an array of Objects into a regular array using IONIC technology?

I'm currently working on converting an Object array into a regular array in HTML without using the "let item of array" method. Despite my extensive googling, I haven't found a solution that works thus far. Why am I avoiding loops? Well, because ...

Utilizing Typescript: Bringing in a module that was exported using module.exports

I am facing an issue with a theme file that needs to be written in ES5: module.exports.theme = { primaryColor: 'blue', borderSize: '2px', // ... } In my attempt to import this theme file in a Typescript file within a c ...

Every day, I challenge myself to build my skills in react by completing various tasks. Currently, I am facing a particular task that has me stumped. Is there anyone out there who could offer

Objective:- Input: Ask user to enter a number On change: Calculate the square of the number entered by the user Display each calculation as a list in the Document Object Model (DOM) in real-time If Backspace is pressed: Delete the last calculated resul ...

When utilizing mergeMap() in conjunction with a subject, the takeWhile() condition set on the observable being merged is disregarded

I am currently exploring ways to halt an observable. After studying blesh's post on github at https://github.com/ReactiveX/rxjs/issues/1542, I believe I am making progress. However, when I apply a switchMap() from my pauser Subject to the takeWhile() ...

Utilizing CucumberJs, Protractor, and TypeScript to Implement Dynamic Tags/Variables in .feature Files

Currently, I am facing a challenge where I have multiple environments with various features enabled. My goal is to streamline the CI/CD process by leveraging the available variables. Is it possible to automate this process by dynamically reading these va ...

Angular 11 encountered an unexpected error: NGCC has encountered a failure

After upgrading my Angular application from version 10 to 11, I encountered a problem. Although the upgrade process was smooth, I am unable to run my application now. When I try to execute ng serve, I receive the following error: Error: Error on worker # ...

Encountering an error while implementing a Typescript addEventListener for keydown events

Struggling with adding and removing event listeners to HTML elements capable of focus, such as buttons. encountering a typescript error specifically related to the lines of code responsible for adding and removing the event listener: focusableElements.fo ...

How come I'm able to access the form's control within setTimeout but not outside of it?

Having just started working with Angular, I came across a strange issue involving forms and setTimeout. When trying to access the form control of an input element inside setTimeout within the OnInit lifecycle hook, it works fine. However, when attempting t ...

Is there a way to revert the ng-eject command in Angular 2 Cli?

After running the ng eject command, I realized that I need to revert it and go back to using regular ng commands. Is there a way to do this without losing any progress? Any help would be greatly appreciated. ...

Toggle bookmark icon styles on click using Angular for the active element

Trying to change the color of a bookmark icon for the current element when clicked, but it ends up affecting all bookmark icons. <div *ngFor="let product of products"> <mat-card class="col-md-3"> <img style="height:100%; width: ...

Obtaining the value from a TypeScript hashmap: Using a getter function or setting a

Looking to create a simple getter for retrieving a value from an object with string keys, or returning a default value of the same type when necessary. This situation arises frequently when working with Maps in Typescript. The main focus is on accessing to ...

Choosing the most suitable stylesheet in Angular 8 when multiple CSS files are present

In my project, I have several CSS stylesheets for angular components. I am curious if there is a method to designate a preferred stylesheet when multiple sheets loaded by a component contain the same styles but with different values. ...

"Error: In TypeScript, the Child Method is not recognized within the Parent

I'm a newcomer to TypeScript and object-oriented programming. I am attempting to use a method from a child class in the parent class, but for some reason, the method is not being recognized. Below is my Child class: import {Ouvrage} from "./Clas ...

Creating HTML dynamic lists that remain fixed to the bottom of the viewport, regardless of any changes in their content

I want to create a dynamic list that displays like this - <div class="list-wrapper"> <ul class="list"> <li class="list-item"> ... </li> <li class="list-item"> ... </ ...

The proper method for incorporating an async function within a for-loop

Recently, I encountered an issue while running the E2E tests after making a small change in the original test code. The change involved adding the checkColumns logic to the test script: it('check search and sort', async () => { await checkL ...

Trouble extracting and utilizing GraphQL queries in Typescript with relay-compiler

I attempted to utilize relay with the Typescript react starter, but I am encountering several problems. It appears that babel-plugin-relay is unable to detect the graphql statements extracted by the relay-compiler. Below is my compiler script: "relay": " ...