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