微信小程序将接收到的数据json化

最近在做一个调用百度接口时,遇到返回过来的值无法加载在页面上,通过查阅官方文档发现是接收到的数据不是json格式的。
下面是遇到问题的代码,console.log(res.data)打印的结果为{"result":"非植物"}

  wx.uploadFile({
              url: 'http://xxxxxx/wx/upload',
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                openID: uid.openid
              },
               success: function(res) {
                console.log(res.data)
                }
          })

以上接收到的数据,要想直接渲染,必须json化。
下面是改进

  wx.uploadFile({
              url: 'http://xxxxxx/wx/upload',
              filePath: tempFilePaths[0],
              name: 'file',
              formData: {
                openID: uid.openid
              },
               success: function(res) {

var result = JSON.parse(res.data);

       			  console.log(res.data)
        }
  })
发布了37 篇原创文章 · 获赞 13 · 访问量 4533

猜你喜欢

转载自blog.csdn.net/qq_42836388/article/details/95652541