Created
May 12, 2018 09:40
-
-
Save nerdic-coder/cd62f8ce313c395e2f293046a4778d80 to your computer and use it in GitHub Desktop.
updateHero.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
/** PUT: update the hero on the server */ | |
updateHero(hero: Hero): Observable<any> { | |
return Observable.create((observer) => { | |
const xhr = new XMLHttpRequest(); | |
xhr.open('PUT', CONFIG.SERVER_URL + `heroes/${hero.id}`, true); | |
xhr.setRequestHeader('Content-type','application/json; charset=utf-8'); | |
xhr.onload = () => { | |
if (xhr.status === 200) { | |
this.messageService.add(`HeroService: updated hero with id:${hero.id}`); | |
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