未捕获的 URIError:URI 格式错误 - Windows 上的 jquery UI 选项卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15326777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught URIError: URI malformed - jquery UI tabs on Windows
提问by Catarina Lou
I'm using jquery UI tabs in a local Windows development environment. I'm testing with their demo code
我在本地 Windows 开发环境中使用 jquery UI 选项卡。我正在用他们的演示代码进行测试
<div id="tabs">
<ul>
<li><a href="#tabs-1">Nunc tincidunt</a></li>
<li><a href="#tabs-2">Proin dolor</a></li>
<li><a href="#tabs-3">Aenean lacinia</a></li>
</ul>
<div id="tabs-1">
<p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. </p>
</div>
<div id="tabs-2">
<p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie</p>
</div>
<div id="tabs-3">
<p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede ve</p>
</div>
</div>
The problem is that I'm getting a "Uncaught URIError: URI malformed" on this function:
问题是我在这个函数上得到一个“Uncaught URIError: URI malformed”:
function isLocal( anchor ) {
return anchor.hash.length > 1 &&
decodeURIComponent( anchor.href.replace( rhash, "" ) ) ===
Uncaught URIError: URI malformed
decodeURIComponent( location.href.replace( rhash, "" ) );
}
I suspect this is due to the URI on windows environment that looks like this:
我怀疑这是由于 windows 环境中的 URI 如下所示:
file:///C:/Work/my%20project/yadda%20ac%E7%F5es%20yadda/submenu.html#tabs-1
How can I have it working on local Windows, since my clients will want to see it working there before going live? I've tested and I know this is being caused by the encoding of the special characters on the uri (like on the word "ac??es"). Is there a way to have it working even with special characters?
我怎样才能让它在本地 Windows 上工作,因为我的客户希望在上线之前看到它在那里工作?我已经测试过,我知道这是由 uri 上特殊字符的编码引起的(比如“ac??es”这个词)。有没有办法让它即使使用特殊字符也能工作?
I know I can fix this simply by removing these characters and renaming the file/folder, but I'd like to have a solution that is more client proof in case they decide to rename the folders again (and everything just goes CABOOM on their faces).
我知道我可以简单地通过删除这些字符并重命名文件/文件夹来解决这个问题,但我想要一个解决方案,该解决方案更能证明客户的身份,以防他们决定再次重命名文件夹(而且他们脸上的一切都变成了CABOOM )。
采纳答案by Peter O.
This is a consequence of the decodeURIComponent method, which assumes that the given string's percent encoded bytes are in UTF-8 (see step 4.d.vii.8 of the Decode operationin sec. 15.1.3 of ECMAScript). Your example string:
这是 decodeURIComponent 方法的结果,该方法假定给定字符串的百分比编码字节为 UTF-8(参见ECMAScript 15.1.3节中的解码操作的步骤 4.d.vii.8 )。您的示例字符串:
"file:///C:/Work/my%20project/yadda%20ac%E7%F5es%20yadda/submenu.html#tabs-1"
Contains the percent-decoded bytes 0xE7 0xF5, which can't occur one after the other in a valid UTF-8 string (they represent the two characters ??
in Windows-1252). Accordingly, decodeURIComponent throws a URIError exception.
包含百分比解码的字节 0xE7 0xF5,它不能在有效的 UTF-8 字符串中一个接一个出现(它们代表??
Windows-1252 中的两个字符)。因此, decodeURIComponent 会抛出一个 URIError 异常。
One way to solve this, which I haven't tested yet, is to ensure that your Web page is served using the UTF-8 character encoding, rather than ISO-8859-1
, which I presume is the default.
解决此问题的一种方法(我尚未测试过)是确保使用 UTF-8 字符编码提供您的网页,而不是使用 UTF-8 字符编码,ISO-8859-1
我认为这是默认设置。
To do this, include a META tag as follows:
为此,请包含一个 META 标记,如下所示:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
(While the ideal thing is to include a Content-Type header containing the charset, this is not generally possible in local testing involving the file system.)
(虽然理想的做法是包含一个包含字符集的 Content-Type 标头,但这在涉及文件系统的本地测试中通常是不可能的。)
And also ensure that the Web pages themselves are encoded in UTF-8.
并且还要确保网页本身以 UTF-8 编码。
Also, where does your example string appear on your Web pages? How is it generated? What browser did you test the behavior on? The answer will help me tell you what to do.
此外,您的示例字符串出现在您的网页上的什么位置?它是如何产生的?您在什么浏览器上测试了行为?答案将帮助我告诉你该怎么做。
回答by TMS
This is a bug in jquery - http://bugs.jqueryui.com/ticket/9518- present for non-UTF8 URLs. I had the same problem and solved it. As I couldn't change the encoding of my page to UTF-8, I finally patched my jqueryui and used Rainer Plumer's solution as a workaround, but I had to use it twice because I have some URI parameters which contain encoded URIs. So my solution was
这是 jquery 中的一个错误 - http://bugs.jqueryui.com/ticket/9518- 存在于非 UTF8 URL。我遇到了同样的问题并解决了它。由于我无法将页面的编码更改为 UTF-8,我最终修补了我的 jqueryui 并使用 Rainer Plumer 的解决方案作为解决方法,但我不得不使用它两次,因为我有一些包含编码 URI 的 URI 参数。所以我的解决方案是
decodeURIComponent( unescape( unescape(s)) )
More generally, there should probably be repeat-until loop, unescaping until you keep getting the same result. This workaround is not a clean solution for sure. The clean solution is asked here:
更一般地说,可能应该有重复直到循环,直到你一直得到相同的结果。这种解决方法肯定不是一个干净的解决方案。这里询问干净的解决方案:
Submit form with get method in non UTF-8 encoding
PS: the entire patch of jquery UI:
PS:整个jquery UI补丁:
function isLocal( anchor ) {
return anchor.hash.length > 1 &&
decodeURIComponent( unescape( unescape( anchor.href.replace( rhash, "" ) ) ) ) ===
decodeURIComponent( unescape( unescape( location.href.replace( rhash, "" ) ) ) );
}
回答by Sebastien Cornu
I had the same error "Uncaught URIError: URI malformed" and I fixed it by doing :
我有同样的错误“未捕获的 URIError:URI 格式错误”,我通过执行以下操作修复了它:
decodeURIComponent(encodeURIComponent(mystring))
decodeURIComponent(encodeURIComponent(mystring))
NB : changing encoding of my page to UTF-8 or adding unescape() were not working for me.
注意:将我的页面编码更改为 UTF-8 或添加 unescape() 对我不起作用。
回答by Rainer Plumer
Something like this ?
像这样的东西?
s = "file:///C:/Work/my%20project/yadda%20ac%E7%F5es%20yadda/submenu.html#tabs-1";
decodeURIComponent( unescape(s) )
回答by Aryeh Beitz
I had this problem myself. I couldn't find a way to clean my source string, so I just bypassed the exception, so the data would continue to load. Hope this helps.
我自己也有这个问题。我找不到清理源字符串的方法,所以我只是绕过了异常,所以数据会继续加载。希望这可以帮助。
try {
myVal = decodeURIComponent(sourceVal);
} catch (err) {
myVal = "";
}
回答by danny117
I cut and pasted the demo code into notepad saved in a folder path that had a space in the folder name (new folder) as test1.html. Then I double clicked test1.html and it worked like a dream.
我将演示代码剪切并粘贴到记事本中,保存在文件夹路径中,文件夹名称(新文件夹)中有一个空格作为 test1.html。然后我双击 test1.html,它就像做梦一样工作。
You obviously changed something and what ever it was change it back it will work fine.
你显然改变了一些东西,无论它是什么改变它都会正常工作。