微信小程序调用后台数据时需要用到wx.request
wx.request({ url: 'http://www.zjkdh.com', data: { "token":'dfcaf2d9f4ac8ff655a802f0ece3573d', "abc":'def' }, method:"POST", header: {'content-type': 'application/x-www-form-urlencoded','headerStr':'fff' },// 默认值 success:function(a) { console.log(a); } }) ;
在后台接收数据之时,需要根据header值的类型来用不同的方式或取。。
header: {'content-type': 'application/x-www-form-urlencoded','headerStr':'fff' }
这样的方式可以获取 header的值,后台获取时可以用 $_POST来获取 data 传输的内容,但这样的话返回值类型是 文本类型的,如果想要转换成数组需要重新转换返回数据。。
header: {'content-type': 'application/json','headerStr':'fff' }
如果header值设置为JSON的话 后台获取data内容时就需要使用 下面的方法来获取
$poststr = file_get_contents('php://input', 'r'); $post = json_decode($poststr, true);
这样获取了 data 传输的内容处理之后再返回JSON格式的数据,wx.request就可以获取JSON格式的数组,可以直接使用了。
获取数据后使用的方法为:
a.data.errorMsg 这直接调用就可以了。
原文地址:http://www.zjkdh.com/library/202101/66.html(张家口导航-睿儿知识库)