javascript 如果我正在编码将用作查询字符串参数的 URI:encodeURI 或 encodeURIComponent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12555230/
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-10-26 16:30:28  来源:igfitidea点击:

If I am encoding a URI which will be used as a query string parameter: encodeURI or encodeURIComponent

javascript

提问by Ryan

From old post: Should I use encodeURI or encodeURIComponent for encoding URLs?, it said:

来自旧帖子:我应该使用 encodeURI 还是 encodeURIComponent 来编码 URL?, 它说:

encodeURI assumes that the input is a complete URI that might have some 
characters which need encoding in it.

encodeURIComponent will encode everything with special meaning, so 
you use it for components of URIs such as

What if I need to encode the URIas query string parameter?

如果我需要对URIas 查询字符串参数进行编码怎么办?

e.g.

例如

var url = "http://example.com/?next=" + encodeURI(url) 

or


var url = "http://example.com/?next=" + encodeURIComponent(url) 

回答by sachleen

If you want to encode a query string, use encodeURIComponent. The reason is simple: among a few other chars, it'll encode the forward slash and amersand that encodeURIwill not.

如果要对查询字符串进行编码,请使用encodeURIComponent. 原因很简单:在其他一些字符中,它将编码不会的正斜杠和 amersand encodeURI

encodeURIComponent

编码URI组件

What's the use? Say you want to encode a URL and pass it in the query string, this will let you encode all the characters so you get something like this:

什么用途?假设您想对 URL 进行编码并将其传递到查询字符串中,这将使您能够对所有字符进行编码,从而得到如下内容:

encodeURIComponent('http://abc.com/my page.html?name=bob&foo=bar')

to get the result

得到结果

"http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar"

You can now safely pass that as a query string like so:

您现在可以安全地将其作为查询字符串传递,如下所示:

http://mysite.com/foo=http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar

Notice how both URLs have a query string parameter foobut that's OK because the encoded URL has that encoded. The entire fooparameter is

请注意两个 URL 如何都有一个查询字符串参数,foo但这没关系,因为编码的 URL 已编码。整个foo参数是

http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar

There is no conflict with the foo=barin the second, encoded, URL.

foo=bar第二个编码的 URL 中的没有冲突。

encodeURI

编码URI

Now, if you want to encode a complete URL that you have already, use encodeURI.

现在,如果您想对已有的完整 URL 进行编码,请使用encodeURI.

encodeURI('http://abc.com/my page.html?name=bob&foo=bar')

Will give you

会给你

"http://abc.com/my%20page.html?name=bob&foo=bar"

Notice how that keeps the URL valid and, in this instance, only encodes the space. If you were to run encodeURIComponenton that, you'd get the mess you see in my first example.

请注意这如何保持 URL 有效,并且在本例中仅对空格进行编码。如果你继续encodeURIComponent这样做,你会得到你在我的第一个例子中看到的一团糟。

What characters are encoded?

哪些字符被编码?

As yabol commented in your first post, this page shows you the Differences between encodeURI, encodeURIComponent, and escape: lower ASCII characters. You notice specifically that encodeURIComponentencodes the following chars that encodeURIdoes not:

正如 yabol 在您的第一篇文章中所评论的,此页面向您展示了encodeURI、encodeURIComponent 和转义之间差异:较低的 ASCII 字符。您特别注意到encodeURIComponent对以下字符进行了编码encodeURI

chr     encodeURI(chr)  encodeURIComponent(chr)
 +           +               %2B
 /           /               %2F
 @           @               %40
 #           #               %23
 $           $               %24
 &           &               %26
 ,           ,               %2C
 :           :               %3A
 ;           ;               %3B
 =           =               %3D
 ?           ?               %3F

Your question

你的问题

You are correct in using encodeURIComponentbecause you're encoding a URL for a query string. This goes back to my first example. If your query-string URL (the one you're encoding) has a query string, you want that to be part of next, not part of your main URL.

您使用正确,encodeURIComponent因为您正在为查询字符串编码 URL。这又回到了我的第一个例子。如果您的查询字符串 URL(您正在编码的那个)有一个查询字符串,您希望它成为 的一部分next,而不是您的主 URL 的一部分。

Wrong

错误的

"http://example.com/?next=" + encodeURI('http://abc.com/my page.html?name=bob&foo=bar')
"http://example.com/?next=http://abc.com/my%20page.html?name=bob&foo=bar"

Your example.com url has two query string parameters: nextand foo

您的 example.com 网址有两个查询字符串参数:nextfoo

Right

"http://example.com/?next=" + encodeURIComponent('http://abc.com/my page.html?foo=bar')
"http://example.com/?next=http%3A%2F%2Fabc.com%2Fmy%20page.html%3Fname%3Dbob%26foo%3Dbar"

Your example.com url contains only one query string parameter: next

您的 example.com 网址仅包含一个查询字符串参数: next

回答by Nokia808Freak

If U need to encode the URIas a query string parameter, then you should definitely go with (2)

如果您需要将 编码URI为查询字符串参数,那么您绝对应该使用(2)

var url = "http://example.com/?next=" + encodeURIComponent(query_url);

take for example google translator, whenever you type the address in the Translate Section, The address is converted to a URI Component and then passed on to the google servers

以谷歌翻译器为例,每当你在翻译部分输入地址时,该地址就会被转换为一个URI组件,然后传递给谷歌服务器

If any URL need to be used as a component, then encodeURIComponent(String);is your best bet

如果需要将任何 URL 用作组件,那么这encodeURIComponent(String);是您最好的选择

回答by daolaf

The second answer in the question you linked already says it pretty clearly: "If you're encoding a string to put in a URL component (a querystring parameter), you should call encodeURIComponent.

您链接的问题中的第二个答案已经说得很清楚了:“如果您要对字符串进行编码以放入 URL 组件(查询字符串参数),则应该调用 encodeURIComponent。

If you're encoding an existing URL, call encodeURI."

如果您正在对现有 URL 进行编码,请调用 encodeURI。”

So in your example encodeURIComponent is the right one.

所以在你的例子中 encodeURIComponent 是正确的。