【问题标题】:cors-anywhere.herokuapp.com not working (503). What else can I try?cors-anywhere.herokuapp.com 不工作 (503)。我还能尝试什么?
【发布时间】:2018-04-15 01:29:57
【问题描述】:

我正在尝试向 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'));
  }

}

【问题讨论】:

  • 特别是关于 Wikipedia,您知道只需将 origin=* 添加到 Wikipedia API URL 的查询参数中,您实际上就可以对 Wikipedia 进行跨域请求吗?见stackoverflow.com/questions/37106041/…

标签: javascript angular heroku cors cors-anywhere


【解决方案1】:

您可以在 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。

【讨论】:

  • 非常感谢您的回答。我认为这是我在这里收到的最完美/最全面的答案...
  • 您如何使用此代理将您自己的域列入白名单?
  • 对于那些尚未安装 heroku 的用户,请在您的终端中运行此程序(在 Mac 上,在其他机器上未经测试):brew tap heroku/brew &amp;&amp; brew install heroku - 可能需要几分钟才能完成
  • 另外我必须在heroku create 之前npm install heroku 安装后打开一个浏览器,我必须在其中创建一个帐户。
猜你喜欢
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-09
  • 2013-05-14
  • 1970-01-01
相关资源
最近更新 更多