在实验一个简单的微信小程序,前台用wx.request请求数据后台php返回一个JSON格式的字符串. 遇到了一个奇葩现象:开发环境显示正常(含有后台传回数据)但真机测试时只显示前台的内容未能显示后台传回的数据。
检查发现小程序后台php返回的JSON结果,在小程序开发环境下能被正确识别为JSON对象, 但真机测试时却被识别为字符串, 而且用JSON.parse(res.data)也不能转换成JSON对象。 有碰到过这个问题的童鞋么? 应该怎么解决呢? 谢谢
前台代码:
wx.request({
url: app.globalData.mallDomain + '/TestPhp.php',
success: function (res) {
console.log(res.data); // 能收到正常的JSON数据, 后续显示正常
var myData = res.data;
if (typeof myData == 'object') {
wx.showModal('这是JSON对象'); //开发环境,到这儿
}
if (typeof myData == 'string') {
wx.showModal('这是JSON string'); //真机环境,走这儿
myData = JSON.parse(res.data); //数据无法转为正常的JSON对象,后续显示崩溃
}
}
})
.......
后台php产生的数据:‘ {"code":0,"data":[{"dateAdd":"2017-04-06 10:16:40","icon":"https://cdn.it120.cc/apifactory/2017/04/23/dcbc320d330a3057e46879598f336da7.jpg","id":35,"isUse":true,"key":"czxf","level":1,"name":"水果","paixu":0,"pid":0,"type":"","userId":51},{"dateAdd":"2017-04-06 10:16:49","icon":"https://cdn.it120.cc/apifactory/2017/04/23/d3d2c6e15e21b8cb6a7bbeabd4da5242.jpg","id":36,"isUse":true,"key":"xhp","level":1,"name":"蔬菜","paixu":0,"pid":0,"type":"","userId":51},{"dateAdd":"2017-04-06 10:17:00","icon":"https://cdn.it120.cc/apifactory/2017/04/23/dad06a09c5ed85b874055d33ba7e312a.jpg","id":37,"isUse":true,"key":"hf","level":1,"name":"副食","paixu":0,"pid":0,"type":"","userId":51}],"msg":"success"}’