ajax请求中文乱码
使用ajax将页面中文数据传送到后台时,往往会出现乱码的情况,解决方法是,在传送前将中文进行编码,然后后台再进行解码:
前台编码
var requestData =window.encodeURI("要传送的中文内容");
`$.ajax({
url: 'http://localhost:8080/index/putData',type: 'GET',//GET
async: false,//或false,是否异步
//contentType:"application/x-www-form-urlencoded;charset=UTF-8",data: {
attr: requestData
},timeout: 5000,//超时时间
success: function (data) {
console.log(data);
responseData=data;
sendResponse(responseData);
}
})
`
ajax请求只需一次编码,如果是get请求,可能还需要再进行一次编码:
str=encodeURI(str)
后台解码
String attr=httpServletRequest.getParameter("arrt"); attr= URLDecoder.decode(attr,"UTF-8");