use*_*257 4 javascript heroku cors angular cors-anywhere
我正在尝试向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'));
}
}
Run Code Online (Sandbox Code Playgroud)
sid*_*ker 15
您可以使用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
Run Code Online (Sandbox Code Playgroud)
运行这些命令后,您将最终使用自己的CORS Anywhere代理运行,例如https://cryptic-headland-94862.herokuapp.com/.因此,不要在您的请求网址前加上https://cors-anywhere.herokuapp.com前缀,而是使用您自己的代理网址为其添加前缀.
| 归档时间: |
|
| 查看次数: |
7992 次 |
| 最近记录: |