javascript decodeURI 没有完全正常工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11323636/
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
decodeURI not fully working
提问by ixchi
I'm trying to remove the URI encoding from a link, but decodeURI doesn't seem to be fully working.
我正在尝试从链接中删除 URI 编码,但 decodeURI 似乎没有完全正常工作。
My example link is this: /linkout?remoteUrl=http%253a%252f%252fsandbox.yoyogames.com%252fgames%252f171985-h-a-m-heroic-armies-marching
我的示例链接是这样的: /linkout?remoteUrl=http%253a%252f%252fsandbox.yoyogames.com%252fgames%252f171985-h-a-m-heroic-armies-marching
After running the JavaScript script, it looks like this:
运行 JavaScript 脚本后,它看起来像这样:
http%3a%2f%2fsandbox.yoyogames.com%2fgames%2f171985-h-a-m-heroic-armies-marching
How can I get rid of the remaining not correct codes in the URI?
如何摆脱 URI 中剩余的不正确代码?
My decoding code:
我的解码代码:
var href = $(this).attr('href'); // get the href
var href = decodeURI(href.substring(19)); // remove the outgoing part and remove the escaping
$(this).attr('href', 'http://'+href) // change link on page
回答by Tobias Krogh
the url looks as it was encoded twice, I also suggest to use decodeURIComponent
url 看起来像被编码了两次,我也建议使用 decodeURIComponent
decodeURIComponent(decodeURIComponent("http%253a%252f%252fsandbox.yoyogames.com%252fgames%252f171985-h-a-m-heroic-armies-marching"))
results in: "http://sandbox.yoyogames.com/games/171985-h-a-m-heroic-armies-marching"
结果:“http://sandbox.yoyogames.com/games/171985-ham-heroic-armies-marching”
but you should check why you have the url encoded twice in advance
但是您应该检查为什么您将 url 提前编码了两次
回答by Michael Blake
I just encountered this situation in an ASHX handler for the PUT verb. ASP.NET is apparently encoding my XML for me, so my server-side call to HttpUtility.UrlEncodewas not needed. Fixing it by calling client-side Javascript decodeURItwice -- is closing the barn door after the cows have already left and the HTTP I was sending was a protocol violation.
我刚刚在 PUT 动词的 ASHX 处理程序中遇到了这种情况。ASP.NET 显然正在为我编码我的 XML,所以不需要我对HttpUtility.UrlEncode 的服务器端调用。通过两次调用客户端 Javascript decodeURI 来修复它——在奶牛已经离开之后关闭谷仓门,我发送的 HTTP 违反了协议。
I would have commented and plus-one'd Tobias Krogh's answer, but I don't have the points to do so…
我会评论并加一分 Tobias Krogh 的回答,但我没有理由这样做......
However, I still think that it is important to note that the failure being discussed here is not a Javascript decodeURI or anything else -- it is a data validation error.
但是,我仍然认为重要的是要注意这里讨论的失败不是 Javascript decodeURI 或其他任何东西——它是数据验证错误。