解决后端接口返回 var obj ="{ name :"你好"}" 类似这样的数据,对象或者数组外面包了一层引号,
把这种数据转成 var obj = { name :"你好"};
直接上代码:
// pages/test/test.js
Page({
jsonStrToJson(jsonStr) {
try {
jsonStr = jsonStr.replace(" ", "");
if (typeof jsonStr != 'object') {
jsonStr = jsonStr.replace(/\ufeff/g, "");
return JSON.parse(jsonStr);
}
} catch (err) {
console.log(err)
}
},
onLoad: function (options) {
wx.request({
url: 'test.php', //仅为示例,并非真实的接口地址
data: {
x: ''
},
header: {
'content-type': 'application/json' // 默认值
},
success(res) {
console.log(res.data)
res.data = this.jsonStrToJson(res.data);
console.log('res.data', res.data);
}
})
},
})