Issue:
I am trying to get data, then send it through a discord webhook. The webhook sends before the request can fully get the data it needs therefor returning undefined. So I tried to put it in a timeout function with a 100ms delay to give the data some time to collect. When doing this I now get a 400 response code. How can this be fixed?
Code:
$.getJSON('link that returns data in json', (data) => {
info = JSON.stringify(data, null, 2)
});
setTimeout(() => {
const request = new XMLHttpRequest();
request.open("POST", "discord webhook link")
request.setRequestHeader('Content-type', 'application/json');
const params = {
username: "Info",
avatar_url: "",
content: `${info}`
}
request.send(JSON.stringify(params));
}, 100);
Any help would be appreciated
EDIT:
I know a timeout function works to help the data load then get it because I tested it with console.log();. It just throws the 400 response when I try doing it with the request in the timeout.