微信小程式POST請求中文亂碼的解決方法
阿新 • • 發佈:2018-12-30
前兩天在整合微信小程式前後端的過程中,出現了中文 亂碼。解決方法如下:
前端的程式碼:
wx.request({ url: '.........', data: { ....... }, header: {'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }, method:'POST', success: function(res) { //請求成功 },)};當為post請求中含中文時,需要加入編碼格式:如UTF-8
在後端,接受請求後,需對請求引數進行解碼
public class StringUtil { public static String decode(String param){ String result= null; try { result = new String(param.getBytes("utf-8"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } }