2017-08-04 70 views
0

我有一个旧的android应用程序,我正在寻找重构到ìonic2来创建一个跨平台的应用程序。角创建交叉起源http请求

在Android应用

,我使用Jsoup通过以下调用Jsoup.connect(url).get();

这工作没有问题,我返回的HTML页面,我可以解析。 不过,如果我尝试这与ionic2(angular2)使用以下

loadTimetableData(url) { 
let headers = new Headers({ 
    'Content-Type': 'application/form-url-encoded;', 
    'Access-Control-Allow-Origin': '*' 
}); 
return this._http.get(url, { headers: headers }).map((res) => res).catch(this._errorHandler); 
} 

我得到的标准CORS错误(index):1 XMLHttpRequest cannot load ${URL}. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 400.

这不是我的服务器,所以我不能添加页眉服务器端。有没有我可以使用其他的图书馆,或者我怎么能解决这个问题,如果在所有

+0

这是不幸的是服务器端的问题,你可以从浏览器修复它绝对没有。我能想到的唯一方法就是使用代理来注入缺少的头文件。 – cgTag

回答

0

你可以尝试,因为我在GET方法

使用这个标题改变头型,并注意您刚才添加;

'Content-Type': 'application/form-url-encoded;', 

这是没有必要,并可能导致错误

尝试改变它。

'Content-Type': 'application/json', 

完整代码

loadTimetableData(url) { 
let headers = new Headers({ 
    'Content-Type': 'application/json', 
}); 
return this._http.get(url, { headers: headers }).map((res) => res).catch(this._errorHandler); 
}