From 5d4a97221d94b9d170e7af4348842ada06ee3dba Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Tue, 20 Jun 2017 17:43:37 -0400 Subject: [PATCH] Make an AJAX request --- starwars/src/app/search/search.component.ts | 4 +++- use_an_api.md | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/starwars/src/app/search/search.component.ts b/starwars/src/app/search/search.component.ts index cbedad5..21a839a 100644 --- a/starwars/src/app/search/search.component.ts +++ b/starwars/src/app/search/search.component.ts @@ -14,7 +14,9 @@ export class SearchComponent implements OnInit { ) { } findCharacter(name){ - console.log('finding ' + name); + this.http.get('http://swapi.co/api/people/?search=' + name) + .toPromise() + .then(response => console.log(response.json())); } ngOnInit() { diff --git a/use_an_api.md b/use_an_api.md index 749ab79..d94065b 100644 --- a/use_an_api.md +++ b/use_an_api.md @@ -156,3 +156,19 @@ constructor( private http: Http ) { } ``` + +## Make an AJAX request + +In `src/app/search/search.component.ts`, `findCharacter(name)` should make a request to `swapi.io`: + +```javascript +findCharacter(name){ + this.http.get('http://swapi.co/api/people/?search=' + name) + .toPromise() + .then(response => console.log(response.json())); +} +``` + +The `rxjs/add/operator/toPromise` import that we previously wrote adds the ability to change the `Observable` (more on this in another lecture) into a `Promise` + +You can test this by looking in the console