前两天在整合微信小程序前后端的过程中,出现了中文 乱码。解决方法如下:
前端的代码:
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; } }