如何在 Javascript 中对 URL 的句点进行编码?

发布于 2024-10-16 15:33:23 字数 401 浏览 2 评论 0原文

下面的 SO 帖子很全面,但是所描述的所有三种方法都无法对句点进行编码。

帖子:在 JavaScript 中编码 URL?

例如,如果我运行三种方法(即 escape、encodeURI、encodeURIComponent),都没有对句点进行编码。

因此“food.store”显示为“food.store”,这破坏了 URL。它会破坏 URL,因为 Rails 应用程序无法将 URL 识别为有效并显示 404 错误页面。也许这是 Rails 路由文件中的配置错误?

使用 JavaScript 对 URL 进行句点编码的最佳方法是什么?

The SO post below is comprehensive, but all three methods described fail to encode for periods.

Post: Encode URL in JavaScript?

For instance, if I run the three methods (i.e., escape, encodeURI, encodeURIComponent), none of them encode periods.

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?

What's the best way to encode periods with Javascript for URLs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

浊酒尽余欢 2024-10-23 15:33:23

我知道这是一个旧线程,但我在这里没有看到任何导致原始问题的 URL 示例。几天前,我自己在使用 Java 应用程序时也遇到了类似的问题。就我而言,带有句点的字符串位于 URL 路径元素的末尾,例如。

http://myserver.com/app/servlet/test.string

在这种情况下,我使用的 Spring 库只是将该字符串的“测试”部分传递给我的控制器类的相关带注释方法参数,大概是因为它将“.string”视为文件扩展名并将其剥离。也许这与上面的原始问题是相同的根本问题?

不管怎样,我只需在 URL 中添加一个尾部斜杠就可以解决这个问题。只是把它扔在那里以防对其他人有用。

约翰

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.

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?

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.

John

不再让梦枯萎 2024-10-23 15:33:23

句点不应该破坏网址,但我不知道你如何使用句点,所以我不能说。我所知道的函数都没有对“.”进行编码对于 url,这意味着您必须使用自己的函数来对“.”进行编码。

您可以对数据进行 Base64 编码,但我不相信在 js 中有一种原生方法可以做到这一点。您还可以在客户端和服务器端将所有句点替换为其 ASCII 等效项 (%2E)。

基本上,通常不需要对“.”进行编码,因此如果您需要这样做,则需要提出自己的解决方案。您可能还需要进行进一步的测试以确保“.”实际上会破坏网址。

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 '.' .

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.

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

苯莒 2024-10-23 15:33:23

我遇到了同样的问题,我的 .htaccess 用 .htaccess 破坏了输入值。
由于我不想更改 .htaccess 正在执行的操作,因此我使用它来修复它:

var val="foo.bar";
var safevalue=encodeURIComponent(val).replace(/\./g, '%2E');

这会执行所有标准编码,然后替换 .htaccess 。其中有相当于 %2E 的 ascii。 PHP 自动转换回 .在 $_REQUEST 值中,但 .htaccess 并不将其视为句点,所以一切都很好。

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:

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.

也只是曾经 2024-10-23 15:33:23

句点不必在 URL 中进行编码。 这里是要查看的 RFC。

如果句点“破坏”了某些内容,则可能是您的服务器正在对 URL 进行自己的解释,这当然是一件好事,但这意味着您必须想出一些自己的编码方案您自己的元字符需要转义。

Periods do not have to be encoded in URLs. Here is the RFC to look at.

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.

涫野音 2024-10-23 15:33:23

我有同样的问题,也许我的解决方案将来可以帮助其他人。

在我的例子中,url 是使用 javascript 生成的。句点用于分隔 url 中的值(sling 选择器),因此选择器本身不允许有句点。

我的解决方案是将所有句点替换为 html 实体,如图 1 所示:

图 1:解决方案

var urlPart = 'foo.bar';
var safeUrlPart = encodeURIComponent(urlPart.replace(/\./g, '.'));

console.log(safeUrlPart); // foo%26%2346%3Bbar
console.log(decodeURIComponent(safeUrlPart)); // foo.bar

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.

My solution was to replace all periods with the html entity as is Figure 1:

Figure 1: Solution

var urlPart = 'foo.bar';
var safeUrlPart = encodeURIComponent(urlPart.replace(/\./g, '.'));

console.log(safeUrlPart); // foo%26%2346%3Bbar
console.log(decodeURIComponent(safeUrlPart)); // foo.bar
望喜 2024-10-23 15:33:23

我在其余 api url 中遇到了 .s 问题。事实上,它们被解释为扩展,以它自己的方式是有意义的。转义没有帮助,因为它们在调用之前没有转义(如前所述)。添加尾随 / 也没有帮助。我通过将值作为命名参数传递来解决这个问题。例如 api/Id/Text.string 到 api/Id?arg=Text.string。您需要修改控制器上的路由,但处理程序本身可以保持不变。

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.

苏辞 2024-10-23 15:33:23

如果可能的话,使用 .htaccess 文件会让事情变得非常酷和简单。只需在句号前添加 \ 即可。类似于:\.

If its possible using a .htaccess file would make it really cool and easy. Just add a \ before the period. Something like:\.

淡看悲欢离合 2024-10-23 15:33:23

这是一个 Rails 问题,请参阅 Rails REST 路由:资源项 ID 以获得解释(以及 Rails 路由指南,第 3.2 节)

It is a rails problem, see Rails REST routing: dots in the resource item ID for an explanation (and Rails routing guide, Sec. 3.2)

东北女汉子 2024-10-23 15:33:23

您不应该使用 encodeURI()无论如何,encodeURIComponent()

console.log(encodeURIComponent('%^&*'));

输入:%^&*。输出:%25%5E%26*。因此,需要明确的是,这不会转换 *希望您在运行 rm *< 之前了解这一点/code> 在“清理”服务器端的输入之后!

幸运的是,MDN 为我们提供了一个解决方法来解决这个明显的问题,fixedEncodeURI()fixedEncodeURIComponent(),它基于此正则表达式:[!'()*]。 (来源:MDN Web 文档:encodeURIComponent( )。)只需重写它以添加句点即可:

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

console.log(fixedEncodeURIComponent('hello.'));

You shouldn't be using encodeURI() or encodeURIComponent() anyway.

console.log(encodeURIComponent('%^&*'));

Input: %^&*. Output: %25%5E%26*. So, to be clear, this doesn't convert *. Hopefully you know this before you run rm * after "cleansing" that input server-side!

Luckily, MDN gave us a work-around to fix this glaring problem, fixedEncodeURI() and fixedEncodeURIComponent(), which is based on this regex: [!'()*]. (Source: MDN Web Docs: encodeURIComponent().) Just rewrite it to add in a period and you'll be fine:

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

console.log(fixedEncodeURIComponent('hello.'));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文