由于CORS问题,get
中的getNews function
方法在本地主机上可以借助代理正常工作,但是,在使用.apk
从设备进行测试或通过模拟网站进行测试时如果使用Android Studio或XCode,则map方法的响应为空。
问题可能是什么原因?
import { Headers , Response } from "@angular/http";
export class NewsService {
getNews(url) {
let url = 'https://admin:password@portal-domain.com:4444/news/_search';
const headers = new Headers({'Content-Type': 'application/json', withCredentials: true});
return this.http.get(url, {headers: headers})
.map((response: Response) => {
let body = response.json();
if (body) {
return body.data || body;
} else {
return {};
}
});
}
}
export class NewsPage {
newsArr;
constructor(private newsService: NewsService);
newsLoad() {
this.newsArr = [];
this.newsService.getNews(this.url)
.subscribe(console.log);
}
文件:ionic.config.json
{
"name": "my_app",
"integrations": {
"cordova": {}
},
"type": "ionic-angular",
"proxies": [
{
"path":"/_search",
"proxyUrl": "https://admin:password@portal-domain.com:4444/news/_search"
}
]
}
“ https://admin:password@portal-domain.com:4444/news/_search”中的邮递员标题
content-type →application/json; charset=UTF-8
content-encoding →gzip
content-length →97562