2017-07-31 91 views
1

我很新的http请求,并试图通过https://darksky.net/dev/docs/faq获取数据。他们不支持CORS,并建议我必须使用代理服务器来获取他们的数据。如何使用我的http请求配置代理?

我使用的角度和我使用的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); 
    } 
} 

如何使用代理服务器的配置呢?

有用的额外信息需要:我应该使用哪些代理服务器服务进行开发?

+2

如上所述,您需要代理**服务器**。你不能在客户端做到这一点。 –

+0

只显示我有多少不明白。我怎样才能达到我的目标?你能指点我一些教程或我应该阅读的来源吗? – Jonathan002

+0

所以这个问题与Angular完全不相关。谷歌的'Cors代理',并选择一个适合您的服务器设置。 – estus

回答

1

感谢您对'Cors Proxy'的评论。

我发现了服务:http://cors-proxy.htmldriven.com,并能够通过使用他们的网址从darksky获取数据。

private weatherUrl = 'http://cors-proxy.htmldriven.com/?url=https://api.darksky.net/forecast/[key]/[latitude],[longitude]'