Solid javascript encode uri
Solid javascript encode uri
我读过这篇文章和更多文章:
什么时候应该使用 escape 而不是 encodeURI/encodeURIComponent?
我仍然没有找到可靠的编码/解码 uri 解决方案。
假设我有这些变量
var first = 'Hello&and';
var second = "Let's have cite";
var third = 'And "hash,.#$';
var fourth = 'åäö other strange chars';
未编码的网址将是:
var url 'http://example.com/?first=' + first + '&second=' + second + '&third=' + third + '&fourth=' + fourth;
稍后它应该在 ajax 请求中,例如:
xhr = new XMLHttpRequest();
xhr.open('POST', url );
我试过这个:
var url 'http://example.com/?first=' + encodeURIComponent(first);
但它不适用于#
.那么什么是适用于所有字符的可靠编码解决方案?
我不使用 jQuery,只使用 javascript。
在编码 uri 参数时使用encodeURIComponent
。当您使用该函数对主题标签进行编码时,它将产生字符串"%23"。
所以在你的例子中:
var first = 'Hello&and';
var second = "Let's have cite";
var third = 'And "hash,.#$';
var fourth = 'åäö other strange chars';
var url = 'http://example.com/?first=' + encodeURIComponent(first) + '&second=' + encodeURIComponent(second) + '&third=' + encodeURIComponent(third) + '&fourth=' + encodeURIComponent(fourth);
将导致包含以下字符串的 url 变量:
http://example.com/?first=Hello%26and&second=Let's%20have%20cite&third=And%20%22hash%2C.%23%24&fourth=%C3%A5%C3%A4%C3%B6%20other%20strange%20chars
有关encodeURIComponent
功能的更多信息,请参见此处。
(引自W3学校)此函数对特殊字符进行编码。在 此外,它还编码以下字符:、/?: @ & = + $ #
您可以尝试使用escape()
函数。这救了我很多次。
escape('#')
确实会产生%23
。
相关文章:
- 如何在Edge中下载图像/png数据URI
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- Ionic Android“;不支持重定向_ uri”;脸书
- Jquery获取请求Uri太长
- 片段uri's未在浏览器上执行
- 无声声音数据uri
- Fin上传器直接上传到Azure Blob存储-在Javascript中获取文件uri
- Facebook身份验证redirect_uri不是't是绝对URI
- canvas没有从uri中绘制完整的图像
- 有什么方法可以调用外部URI方案吗
- Interent Explorer中的数据URI文件下载
- 追加子数据 URI,替换 IE 中的其他对象
- 节点.js请求库的相对 URI
- 将 JSON 下载为文件:IE-11 抛出 414 请求 URI 太大
- 由于重定向URI无效,Key斗篷令牌请求被拒绝
- PHP(PDO)url编码如何在javascript中解码?decodeURI?或解码URI组件
- 将数据URI保存在cookie中,以便稍后检索
- 如何将URI参数转换为JSON
- 字符串的URI转义
- Solid javascript encode uri