Perform An Http Request In The Background Without Loading Webpage?
So there is this 3rd party website that contains a button that, when clicked, sends a GET request and performs some server-side task which updates the logged in user's data into it
Solution 1:
- Generate array of dates for which you want to call.
- Iterate each date, and make a request to the server.
- On completion of the request, pick next date and process it.
Following is the sample code, which you can modify according to your case.
var url="https://something.com/somepath/47240/6/sort?date=";
var date1 = newDate();
var date2 = newDate(2018, 1, 1);
var day;
var between = [date1];
//Generate the date array for which you want to runwhile(date2 <= date1) {
day = date1.getDate()
date1 = newDate(date1.setDate(--day));
between.push(date1);
}
//Covert date to string format for passing to the request urlfunctionformatDate(date) {
var d = newDate(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
var callPage = function (dt) {
returnnewPromise(function (resolve, reject) {
var xhr = newXMLHttpRequest();
var urlWithParam=url+ dt + "&crumb=JMBuREVDPqS";
xhr.open('GET', urlWithParam, true);
xhr.onload = function () {
//Resolve the promise, so that next can be pickedresolve();
};
});
};
//Make the call for each date.for (var i = 0; i < between.length; i++) {
callPage(between[i]).then(function () {
});
}
If you are facing issue with CROSS DOMAIN
request, you can achieve this using hidden iframe
like following.
var url="https://something.com/somepath/47240/6/sort?date=";
var date1 = newDate();
var date2 = newDate(2018, 1, 1);
var day;
var between = [date1];
//Generate the date array for which you want to runwhile(date2 <= date1) {
day = date1.getDate()
date1 = newDate(date1.setDate(--day));
between.push(date1);
}
//Covert date to string format for passing to the request urlfunctionformatDate(date) {
var d = newDate(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
var callPage = function (dt) {
var iframe = document.createElement('iframe');
var urlWithParam=url+ formatDate(dt) + "&crumb=JMBuREVDPqS";
console.log("Request ->",urlWithParam);
iframe.style.display = 'none';
iframe.src = encodeURI(urlWithParam);
document.body.appendChild(iframe);
}
//Make the call for each date.for (var i = 0; i < between.length; i++) {
callPage(between[i]);
}
Solution 2:
You can do something like this
1) Set interval to call the function once in a day
2) The date has to be dynamically increased after calling the function each day
var dayInMilliseconds = 1000 * 60 * 60 * 24;
var todayDate = new date();
functionformatDate(todayDate ) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
setInterval(function() {
$.get("https://something.com/somepath/47240/6/sort",
{"date":formatDate(todayDate),"crumb":"JMBuREVDPqS"},
function(response){
console.log("http request loaded in the background")'
});
},dayInMilliseconds );
Solution 3:
Use Ajax
var http = new XMLHttpRequest();
var url = "Put url here";
varparams = 'Put Params here'
http.open("POST", [url,params].join('?'), true);
Post a Comment for "Perform An Http Request In The Background Without Loading Webpage?"