javaWeb hyperlink (href) request - special character handling

Recently in the project, I encountered a problem, when I clicked a hyperlink, the page reported an error. Through browser debugging, you can know that the sent request parameters are incomplete, because the parameters contain special characters. So it's wrong~~

 

The original code cannot send parameters containing special characters correctly:

<a href="<%=request.getContextPath()%>/kmsDisplayModifyMappingAction.do?mapping_id=<%=kmdb.getMapping_id()%>&k_projectname=<%=kmdb.getK_projectname()%>&projectcode=<%=kmdb.getProjectcode()%>" >modify</a>

Among them, k_projectname contains special characters, (official website: www.fhadmin.org) such as #, which cannot be recognized when sending a request and needs to be escaped.

Modified code:

 
<script type="text/javascript">
        /* 超链接请求特殊字符转换 (官网:www.fhadmin.org) */
        function formatSpecial(mapping_id,k_projectname,projectcode){
            //alert(k_projectname);
            var str = k_projectname.replace(/\"/g,'%22').replace(/\#/g,'%23').replace(/\'/g,'%27').replace(/\?/g,'%3F').replace(/\&/g,'%26').replace(/\=/g,'%3D');
            //alert(str2);
            location.href = "<%=request.getContextPath()%>/kmsDisplayModifyMappingAction.do?mapping_id="+mapping_id+"&k_projectname="+str+"&projectcode="+projectcode+"";
        }
    </script>
 
<a href="javascript:void(0);" onclick="formatSpecial('<%=kmdb.getMapping_id()%>','<%=kmdb.getK_projectname()%>','<%=kmdb.getProjectcode()%>')">modify</a>

The main idea is to escape the parameters that need to be passed in the js function before clicking the hyperlink. Here, the escape() function is mainly used in the js function, and then the special characters contained are escaped with the replace() method. Replace, after the processing is completed, send the request again~~~~~

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326941613&siteId=291194637