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