Ajax提交乱码
1. 页面码 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1.1 如下图,传入到后台,显示正常,不为乱码
$.ajax({ url:"/flowctrl/saveMessageAction.do?message="+"姓名", type: "get", //data:{message:aa}, dataType: "text", success: function(data){ alert("发布成功!"); $("#content").html(""); }, error: function(data){ alert("发布失败!"); } });
1.2 如下图,传入到后台,显示为乱码
$.ajax({ url:"/flowctrl/saveMessageAction.do", type: "get", data:{message:"姓名"}, dataType: "text", success: function(data){ alert("发布成功!"); $("#content").html(""); }, error: function(data){ alert("发布失败!"); } });
解决办法:
强制转换为UTF-8
$.ajax({ url:"/flowctrl/saveMessageAction.do", type: "get", data:{message:encodeURI("姓名",UTF-8")}, contentType: "application/x-www-form-urlencoded; charset=utf-8", dataType: "text", success: function(data){ alert("发布成功!"); $("#content").html(""); }, error: function(data){ alert("发布失败!"); } });
后台代码:
接收时也需要强制转换回来,URLDecoder.decode(message,"UTF-8");