微信小程序調用后臺數據時需要用到wx.request
wx.request({ url: 'http://www.wjglgs.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.wjglgs.com/library/202101/66.html(張家口導航-睿兒知識庫)