【发布时间】:2022-01-18 07:28:12
【问题描述】:
我在浏览器中运行了这段代码
<html>
<script type="module">
console.log("working");
var url = "https://slack.com/api/chat.postMessage";
var auth_token = "xoxb-2B"; //Your Bot's auth token
var body = {channel: "ses", text: "testing app"}
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
headers: {
"Authorization": "Bearer " + auth_token,
"Content-Type" : "application/json"
},
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
postData('https://slack.com/api/chat.postMessage', body)
.then(data => {
console.log(data); // JSON data parsed by `data.json()` call
});
</script>
</html>
我来了
CORS 策略已阻止从源“http://127.0.0.1:5500”访问“https://slack.com/api/chat.postMessage”获取:请求标头字段授权不允许预检响应中的 Access-Control-Allow-Headers。
我不明白,我需要以某种方式指定不记名令牌,即使在文档中说将其放在授权标头中,为什么他们不允许这样做?
【问题讨论】:
-
我认为问题在于您是从网络浏览器发出请求。我认为 Slack API 不支持。
标签: javascript fetch-api slack-api