我正在尝试在不使用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: ""
})
})
以及其他许多方式,但它总是失败!有什么问题? 谢谢!
答案 0 :(得分:0)
一个简短的,非面向对象的版本:
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',
}));
}
答案 1 :(得分:-1)
在.send内部,尝试不使用数据构建对象,只需传递JSON.stringify函数即可:
sendWebhook.send(JSON.stringify({
content: "hi",
username: "hello",
avatar_url: ""
})
)
如果在要测试的浏览器中查看devtools,您都可以在“网络”选项卡上看到正在发送的有效负载,并且您拥有的请求有效负载是JavaScript对象,XmlHttpRequest无法解密。