【问题标题】:Why the HTTP request is sent incorrectly?为什么 HTTP 请求发送不正确?
【发布时间】:2015-05-15 12:05:55
【问题描述】:

原始网址片段:{<>"|?(1)20416lol

编码的 URL 片段:

<code>encodeURIComponent('{<>"|?(1)20416lol')
-> %7B%3C%3E%22%7C%3F(1)20416lol</code>

但是 HTTP GET 请求转到另一个 URL。

【问题讨论】:

  • encodeURIComponent 转义除以下字符以外的所有字符:字母、十进制数字、- _ 。 ! ~ * ' ( )
  • 请看截图。 HTML 中的 URL 片段编码正确。但是浏览器发送给服务器的不是编码值。

标签: http url cross-browser uri


【解决方案1】:

查看 encodeURIComponent 上的 moz dev 网络站点

执行以下操作以覆盖默认 encodeURIComponent 函数。

function fixedEncodeURIComponent (str) {
  return encodeURIComponent(str).replace(/[!'()*\|]/g, function(c) {
    return '%' + c.charCodeAt(0).toString(16);
  })
}

【讨论】:

  • 请看截图。 HTML 中的 URL 片段编码正确。但是浏览器发送给服务器的不是编码值。
猜你喜欢
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 2020-07-05
相关资源
最近更新 更多