Skip to content

Instantly share code, notes, and snippets.

@nerdic-coder
Created May 12, 2018 10:42
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/cb1dfaaa313d0676a4afcec6a34d3432 to your computer and use it in GitHub Desktop.
Save nerdic-coder/cb1dfaaa313d0676a4afcec6a34d3432 to your computer and use it in GitHub Desktop.
addHeroServce.ts
/** POST: add a new hero to the server */
addHero (hero: Hero): Observable<Hero> {
return Observable.create((observer) => {
const xhr = new XMLHttpRequest();
xhr.open('POST', CONFIG.SERVER_URL + `heroes`, true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
xhr.onload = () => {
if (xhr.status === 201) {
this.messageService.add(`HeroService: added new hero`);
observer.next(JSON.parse(xhr.responseText));
}
else {
observer.error(xhr.response);
}
};
xhr.send(JSON.stringify(hero));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment