2017-10-15 145 views
0

我正在使用openweather API做一個簡單的天氣應用程序。我面臨的問題是,當我調用API時,URL調用爲localhost:4200/api... URL,我必須僅在不使用本地主機的情況下調用API URL在URL中沒有本地主機的情況下調用API

WeatherService.ts

private _url: string; 

constructor(private http: Http) { 

} 

set url(url: string) { 
    this._url = url; 
} 

getDataForSingleCity(city: string): Promise<any> { 

    this._url = 'api.openweathermap.org/data/2.5/forecast?q=' + city + '&appid=' + key; 

    return this.http.get(this._url) 
        .toPromise() 
        .then(resp => resp.json().data as any) 
        .catch(this.handleErrors); 
} 

city串得到通過輸入字段通過。

+3

您需要爲絕對URI提供一個協議,添加'http [s]://' – jonrsharpe

回答

0

我面臨的問題是,當我調用API的調用網址爲localhost:4200/API ...

原因的網址,您有以下幾點:

this._url = 'api.openweathermap.org/data/2.5/forecast?q=' + city + '&appid=' + key; 

相對

修復

使用絕對URL:

this._url = 'http://api.openweathermap.org/data/2.5/forecast?q=' + city + '&appid=' + key; 
相關問題