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;
}
遇到了同样问题,跟nginx配置有关,看这篇文档https://blog.csdn.net/SFSDB/article/details/139689140?spm=1001.2014.3001.5501解决,是nginx有缓存导致的