作者:mobiledu2502917243 | 来源:互联网 | 2023-02-02 11:37
我可以使用curl发布到我的Slack Incoming Webhook:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello world"}' https://hooks.slack.com/services/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
但是当我使用时
$.ajax({
url: "https://hooks.slack.com/services/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",
data: '{"text": "Hello world"}',
type: "POST",
contentType: "application/json"
})
.done(function (reply) {
console.log("POST to Slack succeeded")
})
.fail(function (xhr, status, errorThrown) {
console.log("Error in POST to Slack: " + errorThrown.toString())
})
在我的应用程序的localhost:8090服务的页面中,我收到错误:
jquery.js:9536 OPTIONS https://hooks.slack.com/services/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
400 (Bad Request)
XMLHttpRequest cannot load https://hooks.slack.com/services/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.
Response for preflight has invalid HTTP status code 400
通常,我页面上的POST将转到我的服务器,该服务器为页面提供服务.所以我认为我的困惑与理解Webhooks如何工作有关.我认为同源政策禁止我发布到任意网址.但是为什么curl命令工作,我如何让我的页面做curl的工作呢?
我是否需要从服务器发送POST,而不是从客户端浏览器发送?
1> Allen Cypher..:
我从Slack的Ben J那里得到了答案.我所要做的就是删除线
contentType: "application/json"
来自Slack团队的精彩,快速的帮助.非常感激.