IONIC 3 CORS ISSUE [英] IONIC 3 CORS ISSUE
问题描述
在尝试对API进行GET调用时,我遇到了Ionic 3的CORS问题。我正在使用Ionic本地服务器,在命令行中为live server运行ionic serve。
I am having a CORS issue with Ionic 3 when attempting to make GET calls to an API. I am using Ionic local server, running ionic serve in the command line for live server.
错误
请求的资源上没有Access-Control-Allow-Origin标头。因此,不允许访问 http:// localhost:8100 。
我尝试使用代理设置更新ionic.config.json,但这似乎不起作用..
I tried updating "ionic.config.json" with proxy setting but that does not seem to be working..
{
"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
I am trying to make calls to this api
请求网址
https:// api -2445582011268.apicast.io
HEADERS
键值
用户密钥YOUR_KEY
接受申请/ json
Request Url https://api-2445582011268.apicast.io HEADERS Key Value user-key YOUR_KEY Accept application/json
主要问题
我的通话失败。如何为此问题创建代理?
Primary Question My calls are failing. How do I create proxy for this issue?
推荐答案
要解决此问题,请更改以下行
To fix this issue, please change the following lines
ionic.config.json
ionic.config.json
{
"name": "projectLeagueApp",
"app_id": "47182aef",
"type": "ionic-angular",
"integrations": {
"cordova": {}
},
"proxies": [
{
"path":"/games",
"proxyUrl": "https://api-2445582011268.apicast.io/games"
}
]
}
你必须删除最后的/ ofproxyUrl。
You have to remove the " / " which is at the end of of "proxyUrl".
我的数据服务
return this.http.get('/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)
在http调用中,网址应以'/ games'开头。 '/ games'因为离子会将 http:// localhost:< port> / games
代理到 https://api-2445582011268.apicast。 io / games
In the http call, the url should begin with '/games'. '/games' because ionic will proxy http://localhost:<port>/games
to https://api-2445582011268.apicast.io/games
请在您的应用程序中使用上述方法进行外部GET和POST调用。
Please use the above method for external GET and POST calls in your application.
谢谢。
这篇关于IONIC 3 CORS ISSUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!