最近在做一个调用百度接口时,遇到返回过来的值无法加载在页面上,通过查阅官方文档发现是接收到的数据不是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)
}
})