我正在尝试向Wikipedia API发送get请求.我发送请求形式一个角度前端所以我正在尝试使用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')); } }
解决方法
您可以使用5个命令将CORS Anywhere服务器部署到Heroku,只需2-3分钟:
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为您的请求URL添加前缀,而是使用您自己的前缀代替前缀代理的URL.