admin管理员组文章数量:1391975
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!!
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!!
Share Improve this question asked Dec 11, 2018 at 14:32 demostanisdemostanis 983 silver badges10 bronze badges 8-
Any error messages? Why without jQuery, since you are using it? have you tried some basic debugging, as in inserting
console.log(...)
statements? – user5734311 Commented Dec 11, 2018 at 14:37 - Oh, yea, sorry, so, The error is an error 400, I read about it but so I don't know the problem, and yes I tried debugging a lot, but still the same... and... I'm not using JQuery.. $ is a selector I made – demostanis Commented Dec 11, 2018 at 14:38
-
Ok, so what is
$("webhook")[0].value
? Does it correctly contain the URL? What does the API expect? Because you're passing an Object tosend()
which in turn contains a JSON String. Seems kind of inconsistent. – user5734311 Commented Dec 11, 2018 at 14:43 - $("webhook")[0].value = "discordapp./api/webhooks***************/*****************webhook******************" – demostanis Commented Dec 11, 2018 at 14:47
- and I already tried by sending in not like an object but it wasnt still working – demostanis Commented Dec 11, 2018 at 14:48
2 Answers
Reset to default 5A 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',
}));
}
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.
本文标签: javascriptSend discord webhook without jQueryStack Overflow
版权声明:本文标题:javascript - Send discord webhook without jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744706238a2620859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论