传递key的时候,为了防止中文乱码,使用encodeURIComponent编码:
<script>
function searchSubmit(){
reg = /[/S]+/;
obj=$("search_word");
if(!obj.value.match(reg)){
alert("请输入搜索的关键词!");
return;
}
location.href="/place/search?id=" + getUrlParam("id") + "&word=" + encodeURIComponent(obj.value);
}
</script>
但在后台java代码直接request.getParameter("word")就已经乱码了,还没调用URLDecoder.decode。
找资料发现调用request.getParameter的时候java会自动对word进行一次解码,所以就乱码。既然java会自动对word进行一次解码,那我们在前台进行2次编码,然后后台URLDecoder.decode(request.getParameter("word"),"utf-8")就可以解决乱码的问题。
前台代码:
<script>
function searchSubmit(){
reg = /[/S]+/;
obj=$("search_word");
if(!obj.value.match(reg)){
alert("请输入搜索的关键词!");
return;
}
location.href="/place/search?id=" + getUrlParam("id") + "&word=" + encodeURIComponent(encodeURIComponent(obj.value));
}
</script>
encodeURIComponent编码 URLDecoder.decode解码乱码的问题
最新推荐文章于 2024-05-01 19:17:00 发布