Javascript url-Encode vs Base64 编码(用法)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10267597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:34:42  来源:igfitidea点击:

url-Encode vs Base64 encoding ( usages)?

javascriptasp.netquery-stringbase64encodeuricomponent

提问by Royi Namir

I was wondering...

我想知道...

(except the issue with the base64's plus'+' sign in query string - which is translated to 'space' and can be solved by %2b):---> which is the preferred way to transfer data in query string?

(除了查询字符串中 base64 的加号“+”符号的问题 - 它被转换为“空格”并且可以通过 %2b 解决):---> 这是在查询字符串中传输数据的首选方式?

Both functions can be used through the JS commands:

这两个函数都可以通过JS命令使用:

  • btoa
  • encodeUriComponent
  • btoa
  • encodeUriComponent

so im asking myself(and you) :

所以我问自己(和你):

whenshould I use what? ( ive always used encodeUriCompoonent- by instinct).

什么时候应该使用什么?(我一直使用 encodeUriCompoonent- 凭直觉)。

the problem that the definitions are different - but the implementations can be similar...

定义不同的问题 - 但实现可以是相似的......

edit

编辑

I think ive found the reason for asking.... ( and why nobody asked it before)

我想我找到了问的原因......(以及为什么之前没有人问过)

enter image description here

在此处输入图片说明

采纳答案by meze

base64is used to transfer binary data. (not supported in IE, cant encode spacial chars.)

base64用于传输二进制数据。(IE 不支持,不能编码空间字符。)

encodeURIComponentonly encodes special characters.

encodeURIComponent只编码特殊字符。

An interesting thing is that you can't apply base64 to unicode strings without encodeURIComponent: https://developer.mozilla.org/en/DOM/window.btoa

有趣的是,如果没有 encodeURIComponent,您不能将 base64 应用于 unicode 字符串:https: //developer.mozilla.org/en/DOM/window.btoa

回答by lucideer

The answer to this is entirely dependent on your server-side application.

对此的答案完全取决于您的服务器端应用程序。

'+'is not translated to 'space'by the client - it is auto-translated to 'space'by some server-side apps, largely for backward-compatibility reasons (conversely, some server-side apps will leave '+'as '+'in compliance with RFC3986).

'+'不会被客户端转换为'space'- 它会被一些服务器端应用程序自动转换为'space',主要是出于向后兼容的原因(相反,一些服务器端应用程序会将'+'保留为'+'符合RFC3986)。

As far as the client is concerned - btoa()and encodeURIComponent()(and encodeURI()and escape()) just encode a string of text into different abstracted strings according to different encoding or escaping algorithms - btoa()usually produces the smallest resultant string using base64encoding but meze's commentre: unicode is worth taking into account here.

就客户端而言 - btoa()and encodeURIComponent()(and encodeURI()and escape()) 只是根据不同的编码或转义算法将文本字符串编码为不同的抽象字符串 -btoa()通常使用base64编码生成最小的结果字符串,但meze 的评论re:unicode 值得考虑账户在这里。

The important thing to note is what your server-side application (some ASP.NET-based setup in your case) then uses to decode that string back to it's original form.

需要注意的重要一点是您的服务器端应用程序(在您的情况下是一些基于 ASP.NET 的设置)然后使用什么将该字符串解码回其原始形式。

回答by Adergaard

fwiw, I use base64 whenever I want to transport anything that CAN be unicode, between a server and a client. urlencode doesn't handle all unicode charachters all that well. It quickly gets a mess with all the percentage signs.

fwiw,每当我想在服务器和客户端之间传输任何可以是 unicode 的东西时,我都会使用 base64。urlencode 不能很好地处理所有 unicode 字符。它很快就会被所有的百分比符号弄得一团糟。

so, in short: expecting unicode input/output, always base64 the transportation.

所以,简而言之:期待 unicode 输入/输出,总是 base64 传输。