无法 POST 从应用到 Slack Incoming Webhook
Can't POST from app to Slack Incoming Webhook
我可以使用 curl POST 到我的 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 的工作原理有关。我认为同源策略禁止我 POSTing 到任意 url。但为什么 curl 命令有效,我如何让我的页面执行 curl 的操作?
我是否需要从我的服务器而不是客户端浏览器发送 POST?
我从 Slack 的 Ben J 那里得到了答案。
我所要做的就是删除行
contentType: "application/json"
来自 Slack 团队的快速帮助。非常感谢。
我可以使用 curl POST 到我的 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 的工作原理有关。我认为同源策略禁止我 POSTing 到任意 url。但为什么 curl 命令有效,我如何让我的页面执行 curl 的操作?
我是否需要从我的服务器而不是客户端浏览器发送 POST?
我从 Slack 的 Ben J 那里得到了答案。 我所要做的就是删除行
contentType: "application/json"
来自 Slack 团队的快速帮助。非常感谢。