Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Created May 12, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdic-coder/b2da4e249705d737305ab47253050bb9 to your computer and use it in GitHub Desktop.
Save nerdic-coder/b2da4e249705d737305ab47253050bb9 to your computer and use it in GitHub Desktop.
getHeroHttp.ts
getHero(id: number): Observable<Hero> {
return Observable.create((observer) => {
const xhr = new XMLHttpRequest();
xhr.open('GET', CONFIG.SERVER_URL + `heroes/${id}`);
xhr.onload = () => {
if (xhr.status === 200) {
this.messageService.add(`HeroService: fetched hero with id:${id}`);
observer.next(JSON.parse(xhr.responseText));
}
else {
observer.error(xhr.response);
}
};
xhr.send();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment