【发布时间】:2018-11-27 13:53:27
【问题描述】:
我们正在尝试向 CouchDB 发送 HTTP 跨域请求。
为了实现这一点,我们使用以下设置设置了 CouchDB(灵感来自 add-cors-to-couchdb tool):
[HTTPD]
enable_cors = true
[CORS]
origins = *
credentials = true
methods = GET, PUT, POST, HEAD, DELETE
headers = accept, authorization, content-type, origin, referer, x-csrf-token
并编写了类似的代码(当然不是硬编码):
<html>
<body>
<script>
fetch("http://acme.org:5984/mydb/323958d9b42be0aaf811a3c96b4e5d9c", {
method: 'DELETE',
headers: {'If-Match': "1-0c2099c9793c2f4bf3c9fd6751e34f95"}
}).then(x => {
if (x.ok) return x.json().then(console.log);
console.error(x.statusText);
});
</script>
</body>
</html>
虽然它适用于GET 和POST,但我们在DELETE 上得到405 Method not allowed。浏览器告诉预检响应(对OPTIONS 请求)不成功,而服务器指示{"error":"method_not_allowed","reason":"Only DELETE,GET,HEAD,POST,PUT,COPY allowed"}。
我们对 CouchDB 2.1.1 和 1.6.1 都进行了尝试。我们还尝试将origins: * 替换为origins: http://localhost:8080(其中8080 是为上述HTML 提供服务的端口)。我们还尝试将credentials 设置为false。
【问题讨论】:
-
请不要使用
origin: *,因为它允许CSRF漏洞利用!
标签: rest cors couchdb http-status-code-405 preflight