【问题标题】:Encode-DecodeAdventure编码-解码冒险
【发布时间】:2023-04-04 21:33:01
【问题描述】:

我尝试使用 encodeURI()encodeURIComponent() 函数在 javascript 中对特殊字符进行编码,并使用 java.net.URLDecoder.decode() 方法对其进行解码,这在 Firefox 中就像一个魅力。但它似乎在 Internet Explorer 中不起作用。是否有任何替代代码可以在两个浏览器上运行相同的代码?

例子:

当我将$%^&作为值传递时,编码后变为%24%25%5E%26。使用java.net.URLDecoder.decode()方法解码后变成$%%5E&

这是实际值-

var str = "$%^&";
var valueJS = encodeURI(str);
var valueJS = encodeURIComponent(valueJS); // to encode even those chars in valueJS that were not encoded by encodeURI()

这是编码值-

String value = "%2524%2525%255E%2526";
while(value.matches(".*%25[A-Za-z0-9]*")) {
     value = value.replace("%25", "%"); // manually trying to achieve %24%25%5E%26
}
value = java.net.URLDecoder(value, "UTF-8");

// I was expecting the decoded value to be $%^&, but it turns out to be $%%5E&

【问题讨论】:

  • “不起作用”是什么意思?你想做什么?
  • 究竟是什么不工作?您看到了哪些问题或错误消息?
  • 我无法使用上面提到的 java API 进行解码
  • 您是这么说的,但是究竟什么不起作用?您看到什么问题或错误消息?会发生什么?
  • 这越来越接近一个好问题了!请添加执行解码的实际 Java 代码行。你在调用哪个URLDecoder.decode() 方法?它是弃用的吗?

标签: java javascript decode encode


【解决方案1】:

修复它。添加了 2 个<meta> 标签

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

并使用了 javascript escape() 而不是 encodeURI()encodeURIComponent()

【讨论】:

    猜你喜欢
    • 2011-04-20
    • 2020-03-20
    • 2012-10-24
    • 1970-01-01
    • 2012-04-21
    • 2017-10-02
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多