【发布时间】:2018-03-28 09:09:49
【问题描述】:
我在尝试对 API 进行 GET 调用时遇到了 Ionic 3 的 CORS 问题。我正在使用 Ionic 本地服务器,在命令行中为实时服务器运行 ionic serve。
错误 请求的资源上不存在
'Access-Control-Allow-Origin'标头。原点'http://localhost:8100'因此不是 允许访问。
我尝试使用代理设置更新ionic.config.json,但这似乎不起作用..
{
"name": "projectLeagueApp",
"app_id": "47182aef",
"type": "ionic-angular",
"integrations": {
"cordova": {}
},
"proxies": [
{
"path":"/games",
"proxyUrl": "https://api-2445582011268.apicast.io/games/"
}
]
}
我的数据服务
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataProvider {
headers = new Headers({'user-key': '1234567890123'});
options = new RequestOptions ({headers: this.headers});
limit: number = 50;
constructor(public http: Http) {
console.log('Hello DataProvider Provider');
}
getGames(genre, offset_num) {
let genre_id = genre;
let offset = offset_num;
return this.http.get('https://api-2445582011268.apicast.io/games/?fields=name,release_dates,screenshots&limit='+this.limit+'&offset='+offset+'&order=release_dates.date:desc&filter[genres][eq]='+genre_id+'&filter[screenshots][exists]', this.options)
}
}
我正在尝试调用这个 api
请求网址
https://api-2445582011268.apicast.io
HEADERS
Key Value
user-key YOUR_KEY
Accept application/json
主要问题 我的电话失败了。如何为此问题创建代理?
【问题讨论】:
-
尝试将
http.get请求中的url改为/games。代理会将其替换为真实的。
标签: json angular cors ionic3 angular-services