javascript - 遍历js中的数组
问题描述
var webhook_array = webhook_url.split(",");
console.log(webhook_array);
function send(item) {
console.log(item)
request.open("POST", item);
request.setRequestHeader('Content-type', 'application/json');
var myEmbed = {
title: embed_title,
color: hexToDecimal(hexcolour),
description: message_content,
footer: {
text: "Powered by Yapplex Tools",
icon_url: avatarurl,
}
}
var params = {
username: webhook_username,
avatar_url: avatarurl,
embeds: [ myEmbed ]
}
request.send(JSON.stringify(params));
}
webhook_array.forEach(send);
function hexToDecimal(hex) {
return parseInt(hex.replace("#",""), 16)
}
}
此代码应通过数组中的每个 webhook 并使用它们发送消息。它将它们打印到控制台,这意味着它们在那里并且可以检测到它们,但只调用了一个 webhook(当使用两个 webhook 进行测试时)
解决方案
有几件事可能会给您带来问题。最值得注意的是,您需要创建一个新的 XMLHttpRequest()。
var webhook_array = webhook_url.split(",");
console.log(webhook_array);
function send(item) {
console.log(item)
/** CREATE REQUEST HERE */
const request = new XMLHttpRequest();
/** SHOULD WORK NOW */
request.open("POST", item);
request.setRequestHeader('Content-type', 'application/json');
// Other code...
request.send(JSON.stringify(params));
}
webhook_array.forEach(send);
您包含的示例还有一些未定义的变量(avatarurl
, webhook_username
)和一个额外的大括号。确保它们在您的代码中的其他地方:它们可能只是文件的一部分,没有出现在您的问题中。希望这可以帮助!
推荐阅读
- python - 将连接的组件分离到多个图像
- qt - Qt - QOpnGLWidget 和普通 QWidget 的性能差异
- html - HTML 不在 localhost 上运行视频
- python - Pandas groupby 创建许多不同的计算列
- python - Python Deepcopy 和 Manual Copy 给出不同的结果
- objective-c - EVP_get_cipherbyname((const char *)[cipherName UTF8String]) 变为 NULL
- dart - StreamBuilder 不监听/更新
- lambda - 使用 Pandas 应用函数更新多列
- r - 在 R 的正则表达式中使用组和反向引用 - stringr::str_replace
- javascript - D3 只在返回或前进到页面时渲染 SVG 图表