如何在 Javascript 中对 URL 的句点进行编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4938900/
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
How to encode periods for URLs in Javascript?
提问by Crashalot
The SO post below is comprehensive, but all three methods described fail to encode for periods.
下面的 SO 帖子很全面,但描述的所有三种方法都无法对句点进行编码。
Post: Encode URL in JavaScript?
For instance, if I run the three methods (i.e., escape, encodeURI, encodeURIComponent), none of them encode periods.
例如,如果我运行三个方法(即escape、encodeURI、encodeURIComponent),它们都不会对句点进行编码。
So "food.store" comes out as "food.store," which breaks the URL. It breaks the URL because the Rails app cannot recognize the URL as valid and displays the 404 error page. Perhaps it's a configuration mistake in the Rails routes file?
所以“food.store”作为“food.store”出现,它破坏了URL。它破坏了 URL,因为 Rails 应用程序无法将 URL 识别为有效并显示 404 错误页面。也许这是 Rails 路由文件中的配置错误?
What's the best way to encode periods with Javascript for URLs?
使用 Javascript 为 URL 编码句点的最佳方法是什么?
采纳答案by superultranova
Periods shouldn't break the url, but I don't know how you are using the period, so I can't really say. None of the functions I know of encode the '.' for a url, meaning you will have to use your own function to encode the '.' .
句号不应该破坏网址,但我不知道你是如何使用句号的,所以我真的不能说。我所知道的所有函数都没有对 '.' 进行编码。对于 url,这意味着您必须使用自己的函数来对 '.' 进行编码。.
You could base64 encode the data, but I don't believe there is a native way to do that in js. You could also replace all periods with their ASCII equivalent (%2E) on both the client and server side.
您可以对数据进行 base64 编码,但我不相信在 js 中有一种本机方法可以做到这一点。您还可以在客户端和服务器端用它们的 ASCII 等价物 (%2E) 替换所有句点。
Basically, it's not generally necessary to encode '.', so if you need to do it, you'll need to come up with your own solution. You may want to also do further testing to be sure the '.' will actually break the url.
基本上,通常不需要对“.”进行编码,因此如果您需要这样做,则需要提出自己的解决方案。您可能还需要进一步测试以确保“.” 实际上会破坏网址。
hth
第
回答by John Rix
I know this is an old thread, but I didn't see anywhere here any examples of URLs that were causing the original problem. I encountered a similar problem myself a couple of days ago with a Java application. In my case, the string with the period was at the end of the path element of the URL eg.
我知道这是一个旧线程,但我在这里没有看到任何导致原始问题的 URL 示例。几天前,我自己在 Java 应用程序中遇到了类似的问题。就我而言,带有句点的字符串位于 URL 路径元素的末尾,例如。
http://myserver.com/app/servlet/test.string
http://myserver.com/app/servlet/test.string
In this case, the Spring library I'm using was only passing me the 'test' part of that string to the relevant annotated method parameter of my controller class, presumably because it was treating the '.string' as a file extension and stripping it away. Perhaps this is the same underlying issue with the original problem above?
在这种情况下,我使用的 Spring 库只是将该字符串的“test”部分传递给我的控制器类的相关注释方法参数,大概是因为它将“.string”视为文件扩展名并剥离它走。也许这与上述原始问题存在相同的潜在问题?
Anyway, I was able to workaround this simply by adding a trailing slash to the URL. Just throwing this out there in case it is useful to anybody else.
无论如何,我可以通过向 URL 添加尾部斜杠来解决此问题。只是把它扔在那里,以防它对其他人有用。
John
约翰
回答by Matthew Cornelisse
I had this same problem where my .htaccess was breaking input values with . Since I did not want to change what the .htaccess was doing I used this to fix it:
我遇到了同样的问题,我的 .htaccess 用 .htaccess 破坏了输入值。因为我不想改变 .htaccess 正在做的事情,所以我用它来修复它:
var val="foo.bar";
var safevalue=encodeURIComponent(val).replace(/\./g, '%2E');
this does all the standard encoding then replaces . with there ascii equivalent %2E. PHP automatically converts back to . in the $_REQUEST value but the .htaccess doesn't see it as a period so things are all good.
这会执行所有标准编码,然后替换 . 与 ascii 等效 %2E。PHP 自动转换回 . 在 $_REQUEST 值中,但 .htaccess 不将其视为句点,因此一切都很好。
回答by Pointy
Periods do not have to be encoded in URLs. Hereis the RFC to look at.
句点不必在 URL 中编码。这是要查看的RFC。
If a period is "breaking" something, it may be that your server is making its own interpretation of the URL, which is a fine thing to do of course but it means that you have to come up with some encoding scheme of your own when your own metacharacters need escaping.
如果一个句点“破坏”了某些东西,则可能是您的服务器正在对 URL 进行自己的解释,这当然是一件好事,但这意味着您必须在以下情况下提出自己的一些编码方案您自己的元字符需要转义。
回答by TJ.
I had the same question and maybe my solution can help someone else in the future.
我有同样的问题,也许我的解决方案将来可以帮助其他人。
In my case the url was generated using javascript. Periods are used to separate values in the url (sling selectors), so the selectors themselves weren't allowed to have periods.
在我的情况下,url 是使用 javascript 生成的。句点用于分隔 url 中的值(吊索选择器),因此选择器本身不允许有句点。
My solution was to replace all periods with the html entity as is Figure 1:
我的解决方案是用 html 实体替换所有句点,如图 1 所示:
Figure 1: Solution
图 1:解决方案
var urlPart = 'foo.bar';
var safeUrlPart = encodeURIComponent(urlPart.replace(/\./g, '.'));
console.log(safeUrlPart); // foo%26%2346%3Bbar
console.log(decodeURIComponent(safeUrlPart)); // foo.bar
回答by user8296471
I had problems with .s in rest api urls. It is the fact that they are interpreted as extensions which in it's own way makes sense. Escaping doesn't help because they are unescaped before the call (as already noted). Adding a trailing / didn't help either. I got around this by passing the value as a named argument instead. e.g. api/Id/Text.string to api/Id?arg=Text.string. You'll need to modify the routing on the controller but the handler itself can stay the same.
我在 rest api urls 中遇到了 .s 问题。事实上,它们被解释为以自己的方式有意义的扩展。转义无济于事,因为它们在调用之前没有被转义(如前所述)。添加尾随 / 也无济于事。我通过将值作为命名参数传递来解决这个问题。例如 api/Id/Text.string 到 api/Id?arg=Text.string。您需要修改控制器上的路由,但处理程序本身可以保持不变。
回答by David Lartey
If its possible using a .htaccess file would make it really cool and easy. Just add a \ before the period. Something like:\.
如果可能的话,使用 .htaccess 文件会让它变得非常酷和容易。只需在句点前加一个\。就像是:\.
回答by user1251840
It is a rails problem, see Rails REST routing: dots in the resource item IDfor an explanation (and Rails routing guide, Sec. 3.2)
这是一个 rails 问题,请参阅Rails REST 路由:资源项 ID中的点以获取解释(和 Rails 路由指南,第 3.2 节)