5

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

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

The 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
5
  • What version of angular 2 do you use? Commented Sep 23, 2016 at 0:13
  • @galvan looks to be 2.0.0 beta .17 but i am using import @angular/http Commented Sep 23, 2016 at 0:16
  • I have the same issue - any luck? Commented Nov 8, 2016 at 12:19
  • @Yuvals I never solved it. I believe it was a problem with http imports. I wasn't very far into the project, so i simply downloaded a started project with http already imported and working... Commented Nov 14, 2016 at 14:46
  • @MalindaMiller I believe I found the problem .. see my answer below. Commented Nov 14, 2016 at 19:14

2 Answers 2

4

This is probably InMemoryWebApiModule.forRoot related issue. This happens when you load in-memory api, but trying to reach undefined url. The way to solve this is by setting passThruUnknownUrl: true in app.module config:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, I was having the same problem and this is definately a working solution. I'll have to look more into why the InMemory Module caused this behavior. stackoverflow.com/questions/41752254/…
1

As you can see, it's a 404 error, analyse your request to the server. 404 error means that what you requested wasn't found.

About how you make the request, try ti use .map instead of toPromise.

Try to not make HTTP requests on the constructor, use ngOnInit instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.