I use CouchDB as a simple backend solution since a few days. I got it up and running including CORS. However when I'm trying to do ajax calls with the method provided by jQuery the response by CouchDB is delivered in chunks (http response header states >> transfer encoding: chunked) which is a problem, because jQuery does not support chunked encoding (without an additional extension like suggested here: jquery support Transfer-Encoding:chunked? how). Is there a way to change the transfer encoding? Searching in the official documentation and the WWW got me no solution. Just adding an additional header field within my list-generation method just results in an internal server error (somehow obvious). Does anyone have clue how to get ajax calls working without using a streaming extension for jQuery?
Asked
Active
Viewed 420 times
1 Answers
0
When trying to do something like GET /{db}/{docid}?open_revs=[list of rev strings]
, I'm pretty sure the only response available from CouchDB is a data stream of type Transfer-Encoding: chunked
.
So, tailored to this specific request for revs, I use this approach:
fetch(urlOfDoc, { open_revs: JSON.stringify([revId, revID2]) })
.then(response => {
const reader = response.body.getReader()
const decoder = new TextDecoder()
let text = ''
return getLine()
function getLine() {
return reader.read().then(({value, done}) => {
if (value) {
text += decoder.decode(value, {stream: !done})
return getLine()
} else {
// Split on the newline, each fourth line is our JSON, e.g.
// --4b08b42f8ccb77cba04ddfe410ae8b15
// Content-Type: application/json
// [empty line]
// [our json]
const lines = text.split('\n')
const revs = []
lines.forEach((line, i) => {
if ((i + 1) % 4 === 0) {
const jsonRev = JSON.parse(line)
revs.push(jsonRev)
}
})
return revs
}
})
}
})

Kevin
- 530
- 4
- 8