angular - 如何使用我的 http 请求配置代理?

标签 angular http proxy same-origin-policy

我对 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/

相关文章:

每次更改检测后都会调用 Angular ngAfterViewInit

http - 任何替代 URL 缩短器的代理来显示 POST 页面结果?

node.js - 如何在 Angular 2 应用程序中包含时刻时区

css - Primeng angular 7 - 未应用 css

css - 将弹出窗口中的文本和图标从右移到左,反之亦然

http - 从 Golang 中的网络/上下文中获取值

javascript - 在现有 json 对象中添加新键(数组)

windows - Git 错误 : fatal: unable to connect a socket (Invalid argument)

iOS Safari开发 ".local"域问题

c - 透明代理 - 如何在不修改的情况下将套接字传递到本地服务器?