表单提交中文乱码以及后台返回中文都变成问号等中文乱码总结,更新中 | 您所在的位置:网站首页 › 网页提交表格是什么原因 › 表单提交中文乱码以及后台返回中文都变成问号等中文乱码总结,更新中 |
1、js layui.table.render 传值中文乱码(表单提交查询请求中文乱码)
一般的传值中文乱码都是请求方式导致的,get请求会中文乱码,而post不会,当然,这是一般情况 小菜鸟一只,记录一个埋了自己的坑,警告不要再犯 这个问题是我修改别人的代码时遇到的,很简单,下面让我们来看代码 用表单提交数据时总是中文乱码 layui.use('table', function() { var table = layui.table; var tableIns = table.render({ elem: '#userlist' // ,height: 'full-280' , url: '$baseUrl/shp/list' //数据接口 , page: true //开启分页 , limits: [20, 30, 60], limit: 20, id: 'tableId', method : 'post', cols: [ [ //表头 { type:'checkbox'} , { type:'numbers', title: 'No'} , { field: 'userName', title: '账号', align: 'center',width:120 } , { field: 'name', title: '姓名', align: 'center', width: 100 } , { field: 'sex', title: '性别', align: 'center', width: 80 } , { field: 'phone', title: '联系电话', align: 'center', width: 110 } , { field: 'shopName', title: '所属', align: 'center' } , { field: 'createTime', title: '时间', align: 'center',sort:true,width:150, templet:"#fmt_date"} , { title: '操作', align: 'center', toolbar: '#toolbar', width: 100 } ] ], done: function(res, curr, count){ console.log(res); // 编辑人员使用 var shopUsersJsonStr = JSON.stringify(res.data); sessionStorage.setItem("shopUsersJsonStr", shopUsersJsonStr); } }); // 查询条件 $("#search").click(function(){ var accountId = $("#accountId").val(); var userName = $("#shopAdminName").val(); alert("userName = "+userName) tableIns.reload({ where: { //设定异步数据接口的额外参数,任意设 username: accountId ,name: userName } ,page: { curr: 1 //重新从第 1 页开始 } }); }); }); 表单中没有设定提交请求的方式 method : 'post', 而默认提交请求的方式是Get ,博主项目的编码格式为UTF-8,就一直出现乱码 因为是改的别人的代码,博主就没怎么注意,结果…… 所以说还是不能粗心大意 2、post请求后台返回前端数据中文乱码(全部变成问号)后台代码: @ResponseBody @RequestMapping(value = "/savaalert", method = RequestMethod.POST, produces = "application/json; charset=utf-8") produces 中前面 “application/json” 是什么取决于前端页面中接收值的 dataType:"json", 设置为什么 原因:在controller中返回json使用了@ResponseBody,而spring源码中@ResponseBody 的实现类其默认的编码是 iso-8859-1,而项目用的编码为utf-8,所以传中文会出现乱码。
|
CopyRight 2018-2019 实验室设备网 版权所有 |