如何编码Javascript中的URL的周期? [英] How to encode periods for URLs in Javascript?

查看:106
本文介绍了如何编码Javascript中的URL的周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



发布:在JavaScript中编码URL



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



所以food.store出来是food.store打破URL它会中断URL,因为Rails应用程序无法将该URL识别为有效,并显示404错误页面。也许这是Rails路由文件中的一个配置错误?



使用JavaScript编码URL的最佳方式是什么?

解决方案

期间不应该打破url,但我不知道你是如何使用这个时期的,所以我不能说真的。没有一个函数,我知道为url编码'。',这意味着你必须使用你自己的函数来编码'。'。



你可以base64对数据进行编码,但我不相信在js中有一种本机的方法。您也可以在客户端和服务器端以ASCII等价物(%2E)替换所有期间。



基本上,通常不需要对'。'进行编码如果你需要这样做,你需要提出自己的解决方案。你可能还要进一步测试,以确保'。'实际上会打破URL。



hth


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?

解决方案

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

这篇关于如何编码Javascript中的URL的周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆