2019-04-21 11:50:48 +08:00

39 lines
4.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="错误提示">错误提示</h2>
<pre><code class="language-javascript">URIError: malformed URI sequence (Firefox)
URIError: URI malformed (Chrome)
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/URIError" title="URIError 对象用来表示以一种错误的方式使用全局URI处理函数而产生的错误。"><code>URIError</code></a></p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>URI 编码和解码不成功。传递给 <a href="Reference/Global_Objects/decodeURI" title="decodeURI() 函数解码一个由encodeURI 先前创建的统一资源标识符URI或类似的例程。"><code>decodeURI</code></a><a href="Reference/Global_Objects/encodeURI" title='encodeURI()  函数通过将特定字符的每个实例替换为一个、两个、三或四转义序列来对统一资源标识符 (URI) 进行编码 (该字符的 UTF-8 编码仅为四转义序列)由两个 "代理" 字符组成)。'><code>encodeURI</code></a><a href="Reference/Global_Objects/encodeURIComponent" title="encodeURIComponent()是对统一资源标识符URI的组成部分进行编码的方法。它使用一到四个转义序列来表示字符串中的每个字符的UTF-8编码只有由两个Unicode代理区字符组成的字符才用四个转义字符编码。"><code>encodeURIComponent</code></a> 或者 <a href="Reference/Global_Objects/decodeURIComponent" title="decodeURIComponent() 方法用于解码由 encodeURIComponent 方法或者其它类似方法编码的部分统一资源标识符URI。"><code>decodeURIComponent</code></a> 这些函数的参数不合法,导致函数无法正确对其进行编解码。</p>
<h2 id="示例">示例</h2>
<h3 id="编码">编码</h3>
<p>编码操作会将每一个字符实例替换为一到四个相对应的 UTF-8 编码形式的转义序列。如果试图编码一个非高-低位完整的代理字符,将会抛出一个 <a href="Reference/Global_Objects/URIError" title="URIError 对象用来表示以一种错误的方式使用全局URI处理函数而产生的错误。"><code>URIError</code></a> 错误,例如:</p>
<pre><code class="language-js example-bad">encodeURI('\uD800');
// "URIError: malformed URI sequence"
encodeURI('\uDFFF');
// "URIError: malformed URI sequence"
</code></pre>
<p>高-低位完整的代理字符是没问题的,例如:</p>
<pre><code class="language-js example-good">encodeURI('\uD800\uDFFF');
// "%F0%90%8F%BF"</code></pre>
<h3 id="解码">解码</h3>
<p>解码操作则是将已经经过编码的 URL 片段中的每一个转义序列替换为相对应的字符。如果相应的字符不存在,就会抛出错误:</p>
<pre><code class="language-js example-bad">decodeURIComponent('%E0%A4%A');
// "URIError: malformed URI sequence"
</code></pre>
<p>输入没问题的话,通常是下面这样:</p>
<pre><code class="language-js example-good">decodeURIComponent('JavaScript_%D1%88%D0%B5%D0%BB%D0%BB%D1%8B');
// "JavaScript_шеллы"</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="Reference/Global_Objects/URIError" title="URIError 对象用来表示以一种错误的方式使用全局URI处理函数而产生的错误。"><code>URIError</code></a></li>
<li><a href="Reference/Global_Objects/decodeURI" title="decodeURI() 函数解码一个由encodeURI 先前创建的统一资源标识符URI或类似的例程。"><code>decodeURI</code></a></li>
<li><a href="Reference/Global_Objects/encodeURI" title='encodeURI()  函数通过将特定字符的每个实例替换为一个、两个、三或四转义序列来对统一资源标识符 (URI) 进行编码 (该字符的 UTF-8 编码仅为四转义序列)由两个 "代理" 字符组成)。'><code>encodeURI</code></a></li>
<li><a href="Reference/Global_Objects/encodeURIComponent" title="encodeURIComponent()是对统一资源标识符URI的组成部分进行编码的方法。它使用一到四个转义序列来表示字符串中的每个字符的UTF-8编码只有由两个Unicode代理区字符组成的字符才用四个转义字符编码。"><code>encodeURIComponent</code></a></li>
<li><a href="Reference/Global_Objects/decodeURIComponent" title="decodeURIComponent() 方法用于解码由 encodeURIComponent 方法或者其它类似方法编码的部分统一资源标识符URI。"><code>decodeURIComponent</code></a></li>
</ul>
</article>