javascript cors-anywhere.herokuapp.com 不工作 (503)。我还能尝试什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47076743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:10:56  来源:igfitidea点击:

cors-anywhere.herokuapp.com not working (503). What else can I try?

javascriptangularherokucorscors-anywhere

提问by user2094257

I am trying to send a get request to the Wikipedia API. I am sending the request form a angular frontend so i'm trying to use the Heroku CORS Anywhere endpoint to avoid CORS issues. For some reason, I'm still getting a 503 response saying no access-control-allow-origin header is present on the requested resource. Any idea why this would happen/what else I can try?

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

My code:

我的代码:

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'));
  }

}

回答by sideshowbarker

You can deploy a CORS Anywhereserver to Heroku in literally just 2-3 minutes, with 5 commands:

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

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

After running those commands, you'll end up with your own CORS Anywhereproxy running at, e.g., https://cryptic-headland-94862.herokuapp.com/. So then instead of prefixing your request URL with https://cors-anywhere.herokuapp.com, prefix it instead with your own proxy's URL.

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