2

I am trying to make a simple http GET request from my Angular2 app:

    this.http.get("https://mybaseurl.com/hello")
  .map(res => res.json())
  .subscribe(
    function(response) { console.log("Success Response" + response)},
    function(error) { console.log("Error happened" + error)},
    function() { console.log("the subscription is completed")}
  );

The Node.js server is configured this way:

app.get('/hello', function(req, res) {
  res.send("hellow world");
});

When I make the call I keep getting this error:

    caused by: unable to parse url 'https://mybaseurl.com/hello'; original error: Cannot read property 'split' of undefined
   at InMemoryBackendService.parseUrl (in-memory-backend.service.ts:518)
    at InMemoryBackendService.handleRequest (in-memory-backend.service.ts:279)
    at InMemoryBackendService.createConnection (in-memory-backend.service.ts:240)

Any idea what am I doing wrong?

edit: pasting the entire class code:

    import {Component} from '@angular/core';
import {Auth} from './auth.service';
import {AuthHttp} from 'angular2-jwt';
import {Http} from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'ping',
  templateUrl: 'app/ping.template.html'
})
export class ApiService {

  API_URL: string = 'https://myapp.herokuapp.com';
  message: string;

  constructor(private auth: Auth, private http: Http, private authHttp: AuthHttp) {}

  public ping() {

    this.http.get(`${this.API_URL}/hello`)
      .map(res => res.json())
      .subscribe((response) => { 
        console.log("Success Response" + response)
        },
        (error) => { console.log("Error happened" + error)},
        () => { console.log("the subscription is completed")}
    ); 
  }    
}

=====> This looks like a HUGE bug in Angular2 - all http requests return null, or an error message describing it is not in the required pattern. I someone has a working demo of HTTP GET I would love to see

16
  • 1
    Doesn't seem to be an issue with the code above. A suggestion - use () => instead of function() because function() will cause this. not pointing to the current class instance anymore. Perhaps you have this already outside the code you posted which causes errors here. Commented Nov 8, 2016 at 9:57
  • I am calling from localhost to heroku app (myherokuapp.com). Is this related? Commented Nov 8, 2016 at 10:01
  • @GünterZöchbauer this didn't help. The very weird thing is what I try to call a working service that returns a JSON, I get: Error with status: 404 Not Found for URL: null Commented Nov 8, 2016 at 10:13
  • Hard to tell without seeing more code. I wouldn't expect to help in above code because there you don't use this (except with this.http(...). Commented Nov 8, 2016 at 10:14
  • 1
    Do you have <base href="/"> in your index.html? Does it work if you use "mybaseurl.com/hello" without the https://? Commented Nov 8, 2016 at 10:15

3 Answers 3

2

It looks like using InMemoryWebApiModule.forRoot(InMemoryDataService) in @NgModule simultaneously with Http.get causes uncovered urls to return null and error.

Setting it this way worked for me: InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true})

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

Comments

1

Maybe it is beacause of your answer from server - you send string to client, but in map function you try to call res.json(). Can you comment map function call?

Comments

0

Check by using arrow function for success and error as below :

this.http.get("https://mybaseurl.com/hello")
  .map(res => res.json())
  .subscribe((response) => { console.log("Success Response" + response)},
    (error) => { console.log("Error happened" + error)},
    () => { console.log("the subscription is completed")}
); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.