2017-08-04 69 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); 
}