cors-anywhere.herokuapp.com 不工作 (503).我还能尝试什么? [英] cors-anywhere.herokuapp.com not working (503). What else can I try?

查看:22
本文介绍了cors-anywhere.herokuapp.com 不工作 (503).我还能尝试什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的代码:

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

}

推荐答案

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

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

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.

After running those commands, you’ll end up with your own CORS Anywhere proxy 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 with your proxy’s URL.

这篇关于cors-anywhere.herokuapp.com 不工作 (503).我还能尝试什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆