Reputation: 98
I'm trying to send a discord webhook message without jQuery. I tried the following:
var sendWebhook = new XMLHttpRequest()
sendWebhook.open("POST", $("webhook")[0].value)
sendWebhook.onload = function() {
if(sendWebhook.status === 200) {
Leaderboard.sendMessage("Webhook sent!")
} else {
Leaderboard.sendMessage("Failed sending webhook...")
}
}
sendWebhook.send({
data: JSON.stringify({
content: "hi",
username: "hello",
avatar_url: ""
})
})
and so much other ways but it always fail! What's the problem? Thanks!!
Upvotes: 0
Views: 7264
Reputation: 51
A short, not object oriented version:
function discord_message(webHookURL, message) {
var xhr = new XMLHttpRequest();
xhr.open("POST", webHookURL, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
'content': message,
'username':'AI',
}));
}
Upvotes: 5
Reputation: 56
Inside of your .send try not building a object with data, just pass the JSON.stringify function:
sendWebhook.send(JSON.stringify({
content: "hi",
username: "hello",
avatar_url: ""
})
)
If you look at your devtools in whichever browser you're testing in, you can see the payload being sent on the Network tab, and your request payload as you have it is a JavaScript object, which XmlHttpRequest cannot decipher.
Upvotes: 0