收藏
回答

request 中的onChunkReceived 乱码

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 Bug onChunkReceived 微信安卓客户端 8.0+ 2.32

  

网页,postman ,接口都表现正常,开启 enableChunked: true,  后就乱码,求解

let task = wx.request({

      url: 'https://xxx/api/chat/generate',

      enableChunked: true,  

      method: "GET",

    })

    task.onChunkReceived((buffer) => {


      // const decoder = new TextDecoder("utf-8");

      // const str = decoder.decode(new Uint8Array(buffer.data))


      const arrayBuffer = buffer.data;

      const uint8Array = new Uint8Array(arrayBuffer);

      let text = String.fromCharCode.apply(null, uint8Array);

 

      console.log('onChunkReceived',text)

    })

chrome表现

postman

后台接口


乱码

回答关注问题邀请回答
收藏

4 个回答

  • Demons
    Demons
    2023-05-20

    请具体描述问题出现的流程,并提供能复现问题的简单代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)。

    2023-05-20
    有用
    回复
  • 木偶人
    木偶人
    2023-10-26

    text = decodeURIComponent(escape(text));

    2023-10-26
    有用
    回复
  • 张凯
    张凯
    2023-05-19

    @OptIn(BetaOpenAI::class)

    @GetMapping("generate", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])

    fun chat(): Flow<String> {


    return chatService.chat().map { chat ->

    var content = ""

    chat.choices.forEach { chatChunk ->

    chatChunk.delta?.content?.apply {

    content += this

    }

    }

    // print("content == $content")

    Base64.getEncoder().encodeToString(content.toByteArray(Charsets.UTF_8))

    }


    }

    前端使用base64界面后就可以了


     const arrayBuffer = buffer.data;

          const uint8Array = new Uint8Array(arrayBuffer);

          let oText = String.fromCharCode.apply(null, uint8Array)


          let text = oText.replaceAll("data:", "")


          let res = ""

          if (text.length > 0{

            res = Base64.decode(text)

          }

          all += res

          this.setData({

            info: all

          })


    2023-05-19
    有用
    回复
  • 鱼饼🦦
    鱼饼🦦
    2023-05-18

    开发者工具的问题,真机不会乱码

    2023-05-18
    有用
    回复 5
登录 后发表内容