问题描述
我正在使用Javascript发送discord Webhook请求,但我不断收到以下错误消息: 在以下位置访问XMLHttpRequest:
“ WEBHOOK URL” 来源“ http://discordfastlist.ml”已被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 - No 'Access-Control-Allow-Origin' header is present
您真正需要做的是执行飞行前响应,并根据您的javascript请求将正确的cors策略附加到您的请求中