首页 > 解决方案 > 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',
}));

标签: javascriptxmlhttprequestdiscordwebhooks

解决方案


您需要能够事先与不和谐的 CORS 政策进行协商,我认为这个人在类似的情况下也遇到了同样的麻烦。Javascript CORS - 不存在“Access-Control-Allow-Origin”标头

您真正需要做的是执行飞行前响应并将正确的 cors-policy 附加到您的请求以及来自您的 javascript 请求


推荐阅读