在jsp页面写如下js方法,type和title的值都为中文,通过struts框架传到后台后中文发生乱码
function search(){
var type = document.getElementById("type").value;var title = document.getElementById("title").value;
//alert(id+"AND"+roleId);
url="/crm/viewNews/searchNews?type="+type+"&title="+title;
window.location.href = url;
}
解决方法如下:
在js代码里面url值引用encodeURI()方法两次:
url="/crm/viewNews/searchNews?type="+encodeURI(encodeURI(type))+"&title="+encodeURI(encodeURI(title));
window.location.href = url;
然后在后台再将传送值转换成utf-8
String title1 = java.net.URLDecoder.decode(title, "UTF-8");
String type1 = java.net.URLDecoder.decode(type, "UTF-8");
这样即可解决该问题
本文介绍了一种在使用Struts框架时解决通过JavaScript前端传递中文参数导致的乱码问题的方法。具体步骤包括使用encodeURI()函数对URL参数进行两次编码,并在后台接收时进行UTF-8解码。
475

被折叠的 条评论
为什么被折叠?



