Home > Technical & Creative Skills > Programming, Servers & Scripts

How to fire postback? (4)


04-09-2021 04:27 PM #1 choose_a_username (Member)
How to fire postback?

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:

Code:
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);
And I've also tried blindly copying and pasting this

Code:
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();
Neither of which seem to work.

Perhaps it's because I'm trying to redirect to a new page at the same time? If this is the case, should I just wait like 300 ms before I redirect?


04-09-2021 05:49 PM #2 jeremie (Moderator)

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


04-09-2021 06:15 PM #3 choose_a_username (Member)

You're the goat ty

Quote Originally Posted by jeremie View Post
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


04-09-2021 08:08 PM #4 jeremie (Moderator)

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:

Code:
if ('sendBeacon' in navigator) {
    /* navigator.sendBeacon() */
} else {
    /* synchronous XMLHttpRequest */
}


Home > Technical & Creative Skills > Programming, Servers & Scripts