我对 http 请求很陌生,正在尝试使用 https://darksky.net/dev/docs/faq 获取数据.他们不支持 CORS,建议我必须使用代理服务器来获取他们的数据。
我正在使用 Angular 并为 http 请求使用服务:
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class ServiceWeather { // Unconventional but I see all my services when typing service
constructor(private http: Http) { }
private weatherUrl = 'https://api.darksky.net/forecast/[key]/[latitude],[longitude]'
public getWeather(): Promise<any> {
return this.http.get(this.weatherUrl)
.toPromise()
.then(response => response.json().data)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
如何用代理服务器配置这个?
需要额外的有用信息:我应该使用哪些代理服务器服务进行开发?
最佳答案
感谢您对“Cors Proxy”的评论。
我找到了服务:http://cors-proxy.htmldriven.com并且能够通过使用他们的 url 从 darksky 获取数据。
private weatherUrl = 'http://cors-proxy.htmldriven.com/?url=https://api.darksky.net/forecast/[key]/[latitude],[longitude]'
关于angular - 如何使用我的 http 请求配置代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409060/