Created
May 12, 2018 10:42
-
-
Save nerdic-coder/cb1dfaaa313d0676a4afcec6a34d3432 to your computer and use it in GitHub Desktop.
addHeroServce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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