Angular 7: The Pitfalls of Incorrect URL Concatenation

After navigating to a different page within my project, an unexpected 404 error with the wrong URL appears in the console. Here's what it looks like:

https://localhost:4420/example.com/api/customers

However, it should actually look like this:

https://example.com/api/customers

I have only targeted the example.com API in my project and nowhere have I specified to use the localhost:4220 API. This confusion is evident in my environment.ts file:

export const environment = {
  production: false,
  baseUrl: 'example.com/',
  apiUrl: 'example.com/api/'
};

When utilizing the service within my component, here's how it is implemented:

export class CustomersComponent implements OnInit  {
  customers: any;

  constructor(private http: HttpClient) { }

  ngOnInit() {
    const token = localStorage.getItem('jwt');
    this.http.get(environment.apiUrl + 'customers', {
      headers: new HttpHeaders({
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json'
      })
    }).subscribe(response => {
      this.customers = response;
    }, err => {
      console.log(err);
    });
  }
}

Upon removing environment.apiUrl + from the code above, a different issue arises:

Request URL: http://localhost:4220/customers

This discrepancy raises the question of where the "localhost:4420/" portion is coming from and where it is being concatenated within the project.

Answer №1

Consider updating the environment file to utilize the https protocol, as demonstrated below -

export const environment = {
  production: false,
  baseUrl: 'https://example.com/',
  apiUrl: 'https://example.com/api/'
};

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

Running PHP scripts within an Angular2 CLI application

I'm currently working on an Angular 2 app using Angular CLI, but I've noticed that the PHP files are not being compiled correctly. I'm curious if the server that is included with Angular CLI supports PHP. If not, do you have any recommendati ...

How do I get rid of an unwanted and vulnerable dependency that NPM installed without my consent?

I have come across a vulnerability in my docker image related to a package that I wish to remove. The problematic package can be found at: Upon navigating through the node_modules folder starting from app/node_modules/resolve/test/resolver/multirepo/pac ...

Access values of keys in an array of objects using TypeScript during array initialization

In my TypeScript code, I am initializing an array of objects. I need to retrieve the id parameter of a specific object that I am initializing. vacancies: Array<Vacancy> = [{ id: 1, is_fav: this.favouritesService.favourites.find(fav = ...

Dynamic Route Matching in NextJS Middleware

Currently, I am in the process of developing a website that incorporates subdomains. Each subdomain is linked to a file-based page using middleware. Take a look at how the subdomains are being mapped to specific pages: app.com corresponds to /home app.com ...

Ensure the box remains unchecked if it is not enabled

I have implemented a tree table in my Angular application. Below is the code snippet: <h5>Checkbox Selection</h5> <p-treeTable [value]="files5" [columns]="cols" selectionMode="checkbox" [(selection)]=&qu ...

Implementing a builder pattern with @Ngrx/Store

Feeling a bit uncertain... I'm wondering if it's suitable, awesome or not advisable to employ a builder pattern to retrieve API response and then apply the builder pattern on that response before storing it in @ngrx/store? And later on when acces ...

Variety of editions tailored to individual clients

In my development of an Angular 6 application that I plan to distribute to multiple clients, there is a need for customization specific to each client while also maintaining common elements. My vision is to organize the directory structure as follows: /s ...

Embed the value of an array in HTML

Upon running console.log, the data output appears as follows: {TotalPendingResponseCount: 0, TotalRequestSendCount: 1, TotalRequestReceivedCount: 1, TotalRequestRejectCount: 3} To store this information, I am holding it in an array: userData : arrayResp ...

When using Angular 2, the array.splice() function is causing the elements to be removed from the

I am currently working with an HTML table that has default checked rows. <table> <tr> <th></th> <th>Id</th> <th>Name</th> <th>Initial</th> </tr> ...

JavaScript alert box

I'm fairly new to the world of web development, with knowledge in CSS & HTML and currently learning TypeScript. I'm attempting to create a message icon that opens and closes a notifications bar. Here's where I'm at so far: document.getE ...

Securing routes in Angular without relying on LocalStorage or Cookies by implementing an Auth Guard

Currently, I am working on implementing an authentication guard in Angular. Instead of the conventional method of checking local storage or cookies to verify user authentication, I am utilizing API endpoints that respond with 200 OK if a httponly cookie co ...

Updating input values in Angular 5 with the currency pipe

Currently, I am utilizing the currency pipe to convert input values in Angular 5. However, I have noticed that it only works after unfocusing the input box. I am seeking a solution where the value is masked as soon as it is entered, similar to the example ...

What is the most effective method to create a versatile function in Angular that can modify the values of numerous ngModel bindings simultaneously?

After working with Angular for a few weeks, I came across a problem that has me stumped. On a page, I have a list of about 100 button inputs, each representing a different value in my database. I've linked these inputs to models as shown in this snipp ...

Adding an object to an array in Postgres with TypeORM

I am currently facing an issue with the column in my Postgres database that has a data type of json. The code snippet for this scenario is as follows: @Column({ type: 'jsonb', nullable: false, default: [] }) us ...

Strategies for handling superfluous or calculated information within Angular form components

I am faced with a challenge in managing informative fields within my component, especially when some inputs are derived from others. For instance, consider an order that includes a product ID and an amount. Here is a scenario: If a product is selected, I ...

Should the null-forgiving operator be avoided when using `useRef`?

Is the following code snippet considered poor practice? const Component: React.FC<{}> = () => { const ref = React.useRef<HTMLDivElement>(null!); return <div ref={ref} />; } I'm specifically questioning the utilization of ...

What are the steps to integrate an in-memory cache in an Angular frontend using GraphQL queries?

In our Angular frontend application, we are utilizing GraphQL queries with Apollo Client to fetch data. We are interested in implementing caching for our data retrieval process. Initially, we will query the data and store it in the cache. Subsequent requ ...

How should a React Testing Library wrapper be properly typed in a TypeScript environment?

There's this code snippet from Kent C. Dodd's library that I find extremely helpful import * as React from 'react' import {render as rtlRender} from '@testing-library/react' import {ThemeProvider} from 'components/theme& ...

What potential outcomes arise from independently initiating multiple components simultaneously?

Here is a scenario where I am able to achieve the following: @NgModule({ imports: [BrowserModule], declarations: [AppComponent, BComponent], bootstrap: [AppComponent, BComponent] <---------- here two components }) By doing this, it will ge ...

Struggling to reset the first item in an HTML select dropdown with Angular - any tips?

I am having an issue with my dropdown in a modal window. When I first open the modal, the dropdown works as expected. However, if I make a selection and then close and reopen the modal, the selected value remains instead of resetting to 'None selected ...