177
rated 0 times
[
183]
[
6]
/ answers: 1 / hits: 17368
/ 9 Years ago, wed, november 11, 2015, 12:00:00
Angular 2 - How do I write a Http get promise?
I'm importing http and want to set the http header with my auth token.
Then I want to write a http get and put the response into a promise to return to the method that calls it.
So far I have this:
import {Http, Headers} from angular2/http;
import {EnvironmentService} from './environmentService';
export class AuthService {
private environmentService: EnvironmentService;
private http: Http;
private header: Headers;
contructor(_environmentService: EnvironmentService, _http: Http, _header: Headers){
this.environmentService = _environmentService;
this.http = _http;
this.header.append('Authorization', '1234');
this.header.append('Content-Type', 'application/json');
}
getSpotifyData = ():Promise<Object> => {
return this.http
.get('http://ws.spotify.com/search/1/track.json?q=foo', {headers:this.header})
.map((response) => {
return response.json()
})
.toPromise();
}
}
Thanks in advance!
More From » angular