【问题标题】:CouchDB using chunked encoding with jQuery ajax()CouchDB 使用带有 jQ​​uery ajax() 的分块编码
【发布时间】:2015-03-20 12:33:38
【问题描述】:

我使用 CouchDB 作为一个简单的后端解决方案已经有几天了。我启动并运行了它,包括 CORS。但是,当我尝试使用 jQuery 提供的方法进行 ajax 调用时,CouchDB 的响应以块的形式传递(http 响应标头状态>>传输编码:分块)这是一个问题,因为 jQuery 不支持分块编码(没有此处建议的附加扩展名:jquery support Transfer-Encoding:chunked? how)。有没有办法改变传输编码?在官方文档和万维网中搜索没有找到解决方案。只是在我的列表生成方法中添加一个额外的标题字段只会导致内部服务器错误(有点明显)。有没有人知道如何在不使用 jQuery 流扩展的情况下让 ajax 调用正常工作?

【问题讨论】:

    标签: javascript jquery ajax couchdb


    【解决方案1】:

    当尝试执行 GET /{db}/{docid}?open_revs=[list of rev strings] 之类的操作时,我很确定 CouchDB 唯一可用的响应是 Transfer-Encoding: chunked 类型的数据流。

    因此,针对这种特定的转速要求,我使用了这种方法:

    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
            }
          })
        }
      })
    

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2010-11-15
      • 2012-10-18
      相关资源
      最近更新 更多