首页 > 解决方案 > 不使用 jQuery 发送不和谐 webhook

问题描述

我正在尝试在没有 jQuery 的情况下发送不和谐的 webhook 消息。我尝试了以下方法:

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: ""
                })
            })

还有很多其他方法,但它总是失败!有什么问题?谢谢!!

标签: javascriptajaxdiscord

解决方案


一个简短的,非面向对象的版本:

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

推荐阅读