jQuery序列化乱码解决
query之提交序列化表单(serialize)及乱码处理
1 提交乱码处理
JSP文件声明如下
- <%@ page language="java" pageEncoding="UTF-8"%>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
永久的处理方式:
修改jquery1.4.3.js源代码解决:
找到:
contentType: application/x-www-form-urlencoded
替换为:
contentType: "application/x-www-form-urlencoded;charset=UTF-8"
要不然,每个请求前都要加
$("#search_btn1").click(function(){ var submitData = $('#searchForm1').serialize(); alert(submitData); $.ajax({ type: "POST", dataType: "json", contentType: "application/x-www-form-urlencoded;charset=UTF-8", url: '${pageContext.request.contextPath}/jsonparam/getForm', data: $('#searchForm1').serialize(), success: function (data) { var f = data.success; if (data.success == true) { var retlist = data.result; alert(retlist.length); } }, error: function(data) { alert("error:"+data.responseText); } }); });
2 从后台返回中文字符串,前台报错或者为乱码的处理
示例代码:添加发送信息前添加头部编码:
response.setCharacterEncoding("UTF-8");
//response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("测试");
如果是用jQuery.form.js提交form所产生的乱码:
找到这个jquery.form.js,将
- var mp = 'multipart/form-data';
修改成:
先搜索到:param:function
将里面的 encodeURIComponent改成
decodeURIComponent