2017-09-27 35 views

回答

0

簡單的HTTP GET請求,可以幫助你

constructor(private http: Http) { } 

getIp(): Promise<Hero[]> { 
    return this.http.get(this.heroesUrl) 
     .toPromise() 
     .then(response => { 
       console.log(response); 
     }) 
     .catch(this.handleError); 
} 

private handleError(error: any): Promise<any> { 
    console.error('An error occurred', error); 
    return Promise.reject(error.message || error); 
} 

你的反應會是這樣

{ 
    "ip": "xxx.xx.xxx.xxx", 
    "city": "Chennai", 
    "region": "Tamil Nadu", 
    "country": "IN", 
    "loc": "10.0833,79.2833", 
    "org": "AS4755 TATA COMM MAINTAINER" 
}