Search code examples
angulartypescripturl-routing

Wrong URL concatenation in Angular 7


When I navigate to a diffrerent page in my project, the console shows a 404 error with wrong url. Here's how it looks like:

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

while it should look like:

https://example.com/api/customers

As I target example.com api, I specified nowhere in the project to use localhost:4220 api.

Here is my environment.ts:

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

And here is how I use the service:

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);
    });
  }
}

if I remove environment.apiUrl + from the code above, I get

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

so from where comes the part "localhost:4420/" and where it is concatenated ?


Solution

  • Try to changes env. file with https protocol, like below -

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