遇到了同样问题,跟nginx配置有关,看这篇文档https://blog.csdn.net/SFSDB/article/details/139689140?spm=1001.2014.3001.5501解决,是nginx有缓存导致的
wx.request()设置了enableChunked,但是收到的结果非流式还是一次性返回呢?wx.request()已经设置了enableChunked:true,但是实际收到的结果是这样的,结果是分段的,但是确实一次性返回: [图片] 用Postman请求又没有问题,返回是分段式返回的 [图片] 大家帮忙看看是哪里有问题呢?基础库版本是2.25.4 代码如下: test() { const that = this; const requestTask = wx.request({ url: 'https://keyue.cloud.baidu.com/online/core/v5/stream/query', method: 'POST', responseType: "arraybuffer", enableChunked: true, //关键!开启流式传输模式 header: { 'content-type': 'application/json', }, header: { token: that.data.token }, data: { "queryText": '', "sessionId": '' }, success: (res) => { console.log("结束----request success", res); }, fail: (err) => { console.log("request fail", err); }, }); // 监听请求头接受事件 requestTask.onHeadersReceived(r => {}); // 监听数据分块接收事件 requestTask.onChunkReceived((response) => { // 收到流式数据,根据返回值进行相对应数据解码 let data16 = that.buf2hex(response.data) let responseText = that.hexToStr(data16) // 将处理好的字符串加入到数据中 console.log(responseText) }); }, buf2hex(buffer) { return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(''); }, hexToStr(hex) { let str = ''; for (let i = 0; i < hex.length; i += 2) { str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)); } return str; }
10-17遇到了同样问题,跟nginx配置有关,看这篇文档https://blog.csdn.net/SFSDB/article/details/139689140?spm=1001.2014.3001.5501解决
requestTask.onChunkReceived 分段接收数据失败?小程序请求后端分段数据,但是requestTask.onChunkReceived接收的是全部成功之后的数据,并没有分段接收,是等请求全部数据回来requestTask.onChunkReceived才打印接收到,怎么解决 text1() { const requestTask = wx.request({ url: BaseUrl+'/chunked', method: 'POST', enableChunked: true, }) requestTask.onHeadersReceived(res => { console.log(res, 'ppp'); }) requestTask.onChunkReceived((response) => { console.log(response); }) },
10-17遇到了同样问题,跟nginx配置有关,看https://blog.csdn.net/SFSDB/article/details/139689140?spm=1001.2014.3001.5501解决
RequestTask.onChunkReceived只执行一次?wx.request({ enableChunked: true }) public ResponseEntity<StreamingResponseBody> getChunkedData() { return ResponseEntity.ok().body(out -> { out.write("This is chunk 1".getBytes()); out.flush(); try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } out.write("This is chunk 2".getBytes()); out.flush(); }); } 按理来说flush了2次,onChunkReceived方法应该回调2次,实际console打印函数只执行了一次,就是全部返回了
10-17