2017-10-15 107 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; 
相关问题