window.location.href传参中文乱码问题

Posted pwang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了window.location.href传参中文乱码问题相关的知识,希望对你有一定的参考价值。

window.location.href="${pageContext.request.contextPath}/story/exportStoryInfo?domainId="+domainIds                       
                        +"&requirementName="+requirementName;

前端页面需求名称输入“4.19活动”,传递到后台时出现中文乱码问题:

解决方案:

             1.jsp页面中修改为

window.location.href="${pageContext.request.contextPath}/story/exportStoryInfo?domainId="+domainIds                       
                        +"&requirementName="+encodeURI(encodeURI(requirementName));

             2.crontroller层,引入import java.net.URLDecoder;:

String requirementName = URLDecoder.decode(request.getParameter("requirementName"), "utf-8"); 获取中文参数

 同时将代码以如下格式书写,不然会报错

          try {
                String requirementName = URLDecoder.decode(request.getParameter("requirementName"), "utf-8");
                StoryFilterVo.setRequirementName(requirementName);
                XXXXX;
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

以上是关于window.location.href传参中文乱码问题的主要内容,如果未能解决你的问题,请参考以下文章

window.location.href后面带参数怎么加

window.location.href跳转传参并接收参数

window.location.href跳转传参并接收参数

window.location.href跳转带中文参数

JS中,location的用法?能不能location到.do?可不可以传参?如何传参?

JS普通浏览器页面传参