Make an AJAX request

master
Matt Huntington 9 years ago
parent 07a1f2fb02
commit 5d4a97221d

@ -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() {

@ -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

Loading…
Cancel
Save