Newbie question here, but how do you fire a postback URL?
I'm trying to make a simple link that fires the postback, and then redirects to another page. I have the postback URL and the transaction ID appended to it correctly and everything, I just don't know how to fire it. Which I'm sure sounds dumb, but I legit can't figure it out.
I've tried loading the postback in an iframe:
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const transaction_id = urlParams.get('tid');
var postbackURL = "http://myglobalpostbackurl.org/aff_lsr?transaction_id=";
var finalLink = postbackURL + transaction_id;
var x = document.createElement("IFRAME");
x.setAttribute("src", finalLink);
document.body.appendChild(x);
const urlParams = new URLSearchParams(queryString);
const transaction_id = urlParams.get('tid');
var postbackURL = "http://myglobalpostbackurl.org/aff_lsr?transaction_id=";
var url = postbackURL + transaction_id;
var req = null;
if(window.XMLHttpRequest) {
req = new window.XMLHttpRequest();
} else {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open('GET', url);
if(typeof success === 'function') { req.onload = success; }
if(typeof fail === 'function') { req.onerror = fail; }
req.send();
If you want to send a postback and at the same time redirect to a new page, you cannot use XMLHttpRequest, as the redirect will stop the execution.
You need to use the navigator.sendBeacon function :
https://developer.mozilla.org/en-US/...tor/sendBeacon
You're the goat ty
Please note though that it is a relatively new function only supported by around 95% of browsers
https://caniuse.com/?search=Navigator.sendBeacon
When sendBeacon is not available, the fallback is usually to do a synchronous xmlhttprequest (with the third parameter async set to false). This is deprecated in new browsers, so you have to use it only for older ones.
https://www.w3schools.com/xml/ajax_x...quest_send.asp
Something like that:
if ('sendBeacon' in navigator) {
/* navigator.sendBeacon() */
} else {
/* synchronous XMLHttpRequest */
}