Angular 2 HTTP GET returns null url

I am trying to make a simple HTTP GET request using angular 2 with Typescript. I get a 404 error with a null url. Below is my component file and the error I am getting.

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { BreadcrumbService } from './breadcrumbService';
import { Http, Response } from '@angular/http';

@Component({
  moduleId: module.id,
  providers: [ BreadcrumbService],
  templateUrl: 'html/appList.html',
  styleUrls: ['css/list.css']

})
export class HealthComponent {
  constructor(private http: Http) {
      this.http.get('http://jsonplaceholder.typicode.com/posts/1')
      .toPromise()
      .then((res: Response) => {
      console.log('RES: ', res);
      })
    .catch((err: Error) => {
      console.log('ERR!!: ', err);
    });
   }
}

Error message:

Response {_body: Object, status: 404, ok: false, statusText: "Not Found",     headers: Headers…}
 _body:Object
 headers:Headers 
 ok:false
 status:404
 statusText:"Not Found"
 type:2
 url:null
 __proto__:Body
+4
source share
2 answers

This is probably a problem with InMemoryWebApiModule.forRoot. This happens when you load the api into memory, but try to reach the URL undefined. The way to solve this is by setting passThruUnknownUrl: truein app.moduleconfig:

InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}) ..

+4
source

As you can see, this is a 404 error, analyze your request to the server. 404 means the requested request was not found.

, , .map toPromise.

HTTP- , ngOnInit.

+1

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


All Articles