小程序的上传文件接口的注意

小程序的上传文件接口的注意

需要对接口返回的数据转换为 JavaScript 对象

// JSON.parse()将JSON格式的数据转换为 JavaScript 对象

JSON.parse(res.data)

  

官方文档

https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html

 wx.chooseImage({
    success (res) {
      const tempFilePaths = res.tempFilePaths
      wx.uploadFile({
        url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址 该接口返回的的JSON
        filePath: tempFilePaths[0],
        name: 'file',
        formData: {
          'user': 'test'
        },
        success (res){
          // 需要JSON.parse 因为上传接口返回的是字符串
          console.log(typeof res.data)
          let data = JSON.parse(res.data)
          //do something
        }
      })
    }
  })

  

  

猜你喜欢

转载自www.cnblogs.com/zelzzz/p/11517637.html
今日推荐