最近在處理小程序,用到了中文傳到后臺(tái)的情況,
前端的代碼:
wx.request({
url: '.........',
data: {
.......
},
header: {
'Content-Type': 'Application/x-www-form-urlencoded;charset=utf-8'
},
method: 'POST',//如果使用 GET本方法也是不好使
success: function(res) { //請(qǐng)求成功
},
)};
當(dāng)為post請(qǐng)求中含中文時(shí),需要加入編碼格式:如UTF-8
在后端,接受請(qǐng)求后,需對(duì)請(qǐng)求參數(shù)進(jìn)行解碼,代碼如下:
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;
}
}