I have issue to make request using requestjs and also do not have an example of using httpClient to get html file from http server… It works with ionic serve however, it doesnt work with android device.
PromiseLottesRequest = new Promise(function(resolve, reject) {
let currentTime = new Date()
let year = currentTime.getFullYear();
let month = currentTime.getMonth() + 1;
let date = currentTime.getDate();
let hour = currentTime.getHours();
let minute = currentTime.getMinutes();
const URL = `http://www.singaporepools.com.sg/DataFileArchive/Lottery/Output/fourd_result_top_draws_en.html?v=${year}y${month}m${date}d${hour}h${minute}m`;
request({url: URL, headers: {'Origin': '*'}}, function (error, response, html) {
let lottes: Lottery[] = [];
if (!error) {
var $ = cheerio.load(html)
$('.tables-wrap').filter(function() {
let data = $(this);
let id = data.find($('th.drawNumber')).text();
let date = data.find($('th.drawDate')).text();
let first = data.find($('td.tdFirstPrize')).text();
let second = data.find($('td.tdSecondPrize')).text();
let third = data.find($('td.tdThirdPrize')).text();
let starterprize = data.find($('tbody.tbodyStarterPrizes > tr > td')).text();
let starters = starterprize.match(/.{1,4}/g);
let consolationprize = data.find($('tbody.tbodyConsolationPrizes > tr > td')).text();
let consolations = consolationprize.match(/.{1,4}/g);
let lotteObject = new Lottery(`${id}`, `${date}`, `${first}`, `${second}`, `${third}`, starters, consolations);
lottes.push(lotteObject);
starters = [];
consolations = [];
});
resolve(lottes);
} else {
reject(error);
}
});
});
ngOnInit () {
this.message = 'Running';
this.PromiseLottesRequest
.then((data: Lottery[]) => {
this.lotteries = data;
this.message = 'Requested';
}).catch((e) => {
this.message = e;
});
}
This is my code. It works running chrome browser by allow-control-allow-origin: * chrome plugin. It doesnt work on android cordova.