【发布时间】:2016-07-07 08:08:50
【问题描述】:
我已经阅读了这篇文章和更多文章:
When are you supposed to use escape instead of 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。
【问题讨论】:
-
据我所知,
encodeURIComponent用于参数。encodeURI用于 URL...i.imgur.com/2xKpYd0.png 在您的情况下,#应使用encodeURIComponent(%23)当你说它不起作用时,你是什么意思? -
您必须更具体地了解“不适用于
#”的含义:encodeURIComponent('And "hash,.#$')→"And%20%22hash%2C.%23%24"。 -
你试过
escape()吗? -
@Krishnakumar_Muraleedharan 不使用转义,转义和取消转义功能已弃用。 developer.mozilla.org/en/docs/Web/JavaScript/Reference/…
标签: javascript url encode