我正在尝试向维基百科API发送获取请求。 我正在从角度前端发送请求,因此我正在尝试使用 Heroku CORS Anywhere 端点来避免 CORS 问题。 出于某种原因,我仍然收到 503 响应,指出请求的资源上不存在访问控制允许源标头。 知道为什么会发生这种情况/我还能尝试什么吗?
我的代码:
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'));
}
}
您可以使用 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
运行这些命令后,您最终将运行自己的 CORS Anywhere 代理,例如 https://cryptic-headland-94862.herokuapp.com/
.因此,与其在请求 URL 前面加上 https://cors-anywhere.herokuapp.com
,不如在它前面加上代理的 URL。
作为回应,我想为Windows用户提供更详细的回应:
窗口请求项
- 创建 Heroku
- 帐户 (heroku.com) 安装 Heroku CLI(例如https://devcenter.heroku.com/articles/heroku-command-line) 安装Git(例如 https://gitforwindows.org/)安装npm工具(例如https://nodejs.org/en/download/)
完成上述所有操作后,发出以下命令
打开一个新终端,然后:
heroku login
git clone https://github.com/Rob--W/cors-anywhere.git
cd cors-anywhere/
npm install
heroku create
git push heroku master
它将处理和更新/上传,您将获得一个应用程序 URL:
https://some-name-giveng.herokuapp.com/
这是因为公共演示服务器 (cors-anywhere.herokuapp.com) 受到 2021 年 1 月的限制 https://github.com/Rob--W/cors-anywhere/issues/301
这是我自己的代理服务器
https://fast-dawn-89938.herokuapp.com/
你可以这样使用:https://fast-dawn-89938.herokuapp.com/https://your-domain.com
演示:https://fast-dawn-89938.herokuapp.com/https://google.com