cors-anywhere.herokuapp.com不工作? [英] cors-anywhere.herokuapp.com not working?
问题描述
我试图向维基百科api发送获取请求。我将请求表单发送给角度前端,因此我试图使用heroku cors-anywhere终端来避免CORS问题。出于某种原因,我仍然收到503响应,说没有访问控制允许来源标题出现在请求的资源。任何想法为什么会发生这种情况/我可以尝试什么?
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?
我的代码:
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'));
}
}
推荐答案
https://cryptic-headland-94862.herokuapp.com/ 是一种替代方案 CORS Anywhere 实例我设置了自己,你可以这样尝试:
https://cryptic-headland-94862.herokuapp.com/ is an alternative CORS Anywhere instance I set up myself that you can try like this:
API_URL: string = 'https://cryptic-headland-94862.herokuapp.com/https://en.wikipedia.org/wiki/';
即在请求URL前加上 https:// cryptic- headland-94862.herokuapp.com /
无论您在哪里尝试 https://cors-anywhere.herokuapp.com/
。
That is, just prefix the request URL with https://cryptic-headland-94862.herokuapp.com/
wherever you were trying https://cors-anywhere.herokuapp.com/
.
然而,我不能保证它不会以相同的方式在有时不可用时出现问题 https://cors-anywhere.herokuapp.com/ 自上月中旬(2017年10月)左右开始。 https://github.com/Rob--W/cors -anywhere / issues / 90#issuecomment-340777667 表明可能是因为它被过度的请求所困扰。
However, I can’t promise it won’t end up having problems with being unavailable at times in the same way https://cors-anywhere.herokuapp.com/ has since somewhere around the middle of last month (October 2017). https://github.com/Rob--W/cors-anywhere/issues/90#issuecomment-340777667 indicates that might be due to it getting hammered with excessive requests.
但无论如何,这就是你'已经获得503s的 https://cors-anywhere.herokuapp.com/
But anyway, that’s the reason you’ve been getting 503s for https://cors-anywhere.herokuapp.com/
由于您无法预测CORS Anywhere实例(或其他第三方替代选项)何时可能无法使用,因此无法在解决此问题时进行修复它是不可用的 - 最好的长期解决方案是使用 CORS Anywhere 实例部署到Heroku中。 W / cors-anywhere /rel =noreferrer> https://github.com/Rob--W/cors-anywhere/ ,您可以在出现问题时控制和修复。
Given you can’t predict when that CORS Anywhere instance (or other third-party alternative) might be unavailable — and there’s nothing you can do to fix it when it is unavailable — the best long-term solution is to deploy your own (private) CORS Anywhere instance to Heroku using the code from https://github.com/Rob--W/cors-anywhere/ that you can control and fix when it has problems.
您可以部署一个 CORS Anywhere 在3到5分钟内,用5条命令就可以使Heroku发挥作用:
You can deploy a CORS Anywhere server to Heroku in literally 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/ 。因此,请不要在 https://cors-anywhere.herokuapp.com
前面加上前缀,而应使用自己的代理网址。
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 instead with your own proxy’s URL.
这篇关于cors-anywhere.herokuapp.com不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!