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 to send() 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
 |  Show 3 more ments

2 Answers 2

Reset to default 5

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',
        }));
    }

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