前几天主管问我FreeMarker下,Url如何进行编码,我当时的想法是 “ 这需要编码么?”,

的确,在绝大部分情况下,浏览器、系统框架能帮我们解决编码的问题,即使不处理也没任何问题,所以我思考了这么个应用场景。

 

Js的话,主要就是encodeURIComponent的使用,他会把路径中的符号、中文进行编码,这样不会与Url中的语法设定冲突

在Java中也有类似的语法,用来解决中文路径、空格等问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
asdad
<a >asdasd</a>
<script type="text/javascript">
    var ur = encodeURIComponent('a=http://www.baidu.com?a=这是一段中文&b=3');
    console.log(ur)
    document.getElementById('test').setAttribute('href', 'http://localhost:8080/Sea/test?' + ur);
</script>
</body>
</html>

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
猜你喜欢
  • 2022-03-10
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-15
  • 2021-06-25
  • 2022-01-08
相关资源
相似解决方案