1. 程式人生 > >微信小程式POST請求中文亂碼的解決方法

微信小程式POST請求中文亂碼的解決方法

前兩天在整合微信小程式前後端的過程中,出現了中文 亂碼。解決方法如下:

前端的程式碼:

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;
}
}