13

我正在尝试向 Wikipedia API 发送获取请求。我正在从一个角度前端发送请求,所以我尝试使用 Heroku CORS Anywhere 端点来避免 CORS 问题。出于某种原因,我仍然收到 503 响应,说请求的资源上没有 access-control-allow-origin 标头。知道为什么会发生这种情况/我还能尝试什么?

我的代码:

import { Injectable } from '@angular/core';
import { Http, Response, } from '@angular/http';
import { Observable } from 'rxjs/Rx';



@Injectable()
export class RestService {
    API_URL: string = 'https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/';

  constructor(private http: Http) { }

  public getRandomArticle() : Observable<any> {
        return this.http.get(`${this.API_URL}Special:Random`)
        .map((res: Response) => res.json())
        .catch((err: any) => Observable.throw(err || 'server error'));
  }

}
4

1 回答 1

67

您可以在 2-3 分钟内将CORS Anywhere服务器部署到 Heroku,只需 5 个命令:

git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master

运行这些命令后,您将拥有自己的CORS Anywhere代理,例如https://cryptic-headland-94862.herokuapp.com/. 因此,不要在您的请求 URL 前加上https://cors-anywhere.herokuapp.com前缀,而是在它前面加上您代理的 URL。

于 2017-11-02T21:50:11.153 回答