javascript - Discord Webhook 访问控制允许来源
问题描述
我正在使用 Javascript 发送 Discord webhook 请求,但我不断收到以下错误:访问 XMLHttpRequest 位于
来自源“http://discordfastlist.ml”的“WEBHOOK URL”已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求中不存在“Access-Control-Allow-Origin”标头资源。
我在谷歌搜索后确切知道这是什么,尝试了我找到的所有解决方案,但似乎没有任何东西可以与 Discord webhooks 一起使用。
我正在使用的代码:
var xhr = new XMLHttpRequest();
xhr.open("POST", "this is the webhook URL, I am not showing this in the code but it is there", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
'content': "test",
'username':'Test Webhook',
}));
解决方案
您需要能够事先与不和谐的 CORS 政策进行协商,我认为这个人在类似的情况下也遇到了同样的麻烦。Javascript CORS - 不存在“Access-Control-Allow-Origin”标头
您真正需要做的是执行飞行前响应并将正确的 cors-policy 附加到您的请求以及来自您的 javascript 请求
推荐阅读
- c# - 如何在 C# 中使用 KeyPress 事件按下 Alt 时显示消息?
- python - 断言 pandas 系列仅包含字符串
- flutter - 用于离线/在线网络套接字同步的 Flutter 设计模式
- node.js - npm-link 库上的无效挂钩调用
- java - 无法在仪器中打开 trace.zip 文件内容
- javascript - Multer req.file 返回未定义
- c++ - (C++) 为什么我的程序没有输出平均等待时间和超过 1 小时的平均时间?
- javascript - Node.js 在 MacOS 中打开新的终端窗口
- c - 如何获得位图图像的分辨率?
- perl - 如何获得代码覆盖率的 MakeMaker 目标?