评论

SSE 接收到的参数怎么转化成字符串

怎么办流式请求返回的数据 转成字符串

// 流式请求

        const requestTask = wx.request({

          url: "",

          enableChunked: true, // 开启流式传输

          header: {

            'Authorization': token,

            "Content-Type": "application/json;charset=UTF-8",

          },

          method: "post",

          responseType: "text",

          data: paramData,

          timeout: 200000,

          success: (res1) => {

            console.info("获取成功: ", res1);

            _this.scrollToBottom();

          },

          fail(err) {

            console.log(err, "err");

            showToast('发送失败,请重试');

          },

        });

        requestTask.onChunkReceived((res) => {

          if (res.data) {

            let data = new TextDecoder().decode(res.data)

            console.log(data)

          }

        });

        requestTask.onHeadersReceived((res) => {

          console.log(res)

        });

      },

      fail: function (err) {

      }

    })


最后一次编辑于  2024-11-26  
点赞 0
收藏
评论

1 个评论

  • Redamancy
    Redamancy
    03-18

    我在   requestTask.onChunkReceived((res) => 后面打印res.data时,出现的是arraybuffer,解析时如果有中文就会乱码,当我检查这个arraybuffer时,发现但是传回的arraybuffer就是错误的,出现了一个[1,34],这种无论用什么转换时都会乱码,不知道是什么原因了,难道是requestTask.onChunkReceived函数本身就有bug?

    03-18
    赞同
    回复
登录 后发表内容