我可以使用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,而不是从客户端浏览器发送?
答案 0 :(得分:7)
我得到了来自Slack的Ben J的回答。 我所要做的就是删除行
contentType: "application/json"
来自Slack团队的精彩,快速的帮助。非常感谢。