Javascript cors-anywhere.herokuapp.com不工作(503)。我还能试什么?
我正在尝试向WikipediaAPI发送get请求。我从一个角度前端发送请求,所以我尝试使用Heroku CORS Anywhere端点来避免CORS问题。出于某种原因,我仍然收到503响应,表示请求的资源上不存在访问控制允许源标题。知道为什么会发生这种情况吗/我还能尝试什么 我的代码:Javascript cors-anywhere.herokuapp.com不工作(503)。我还能试什么?,javascript,angular,heroku,cors,cors-anywhere,Javascript,Angular,Heroku,Cors,Cors Anywhere,我正在尝试向WikipediaAPI发送get请求。我从一个角度前端发送请求,所以我尝试使用Heroku CORS Anywhere端点来避免CORS问题。出于某种原因,我仍然收到503响应,表示请求的资源上不存在访问控制允许源标题。知道为什么会发生这种情况吗/我还能尝试什么 我的代码: import { Injectable } from '@angular/core'; import { Http, Response, } from '@angular/http'; import { Obs
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'));
}
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Response,};
从'rxjs/Rx'导入{Observable};
@可注射()
导出类REST服务{
API_URL:string='1〕https://cors-anywhere.herokuapp.com/https://en.wikipedia.org/wiki/';
构造函数(私有http:http){}
public getRandomArticle():可观察{
返回this.http.get(`${this.API\u URL}特殊:Random`)
.map((res:Response)=>res.json())
.catch((err:any)=>可观察的.throw(err | |'服务器错误');
}
}
您只需2-3分钟即可将CORS Anywhere服务器部署到Heroku,只需5个命令:
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作为前缀。特别是关于Wikipedia,您知道,只需将origin=*
添加到Wikipedia API URL的查询参数中,就可以向Wikipedia发出跨源请求?谢谢你的回答。我认为这是我在这里收到的最完美/最全面的答案…你如何使用这个代理来白名单你自己的域?对于那些没有安装heroku的人,在你的终端上运行这个(在Mac上,未在其他机器上测试):brew点击heroku/brew&&brew安装heroku
-可能需要几分钟才能完成