javascript 在 url 的请求参数中传递“#”哈希符号在 Firefox 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13228835/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:08:25  来源:igfitidea点击:

Passing "#" hash symbol in request parameter of url not working in Firefox

javascriptajaxfirefoxxmlhttprequest

提问by user1697113

I am hitting a struts action using AJAX, everything is fine but there is problem with Firefox , when i am passing the parameter in URL as a request parameter and if that parameter, contains hash(#) symbol in the end, then firefox strips everything after that symbol and send that parameter to action without it.

我正在使用 AJAX 执行 struts 操作,一切都很好,但 Firefox 有问题,当我将 URL 中的参数作为请求参数传递时,如果该参数最后包含 hash(#) 符号,则 Firefox 会删除所有内容在那个符号之后,并在没有它的情况下将该参数发送到操作。

For example, if im passing test123#abcd in Firefox, then i am getting only test123 in action class as opposed to test123#abcd which is undesirable for my requirement.For IE it is working perfectly.Is there any way by which i can extract the full parameter including the # symbol in Firefox.

例如,如果我在 Firefox 中通过了 test123#abcd,那么我在操作类中只得到 test123,而不是 test123#abcd,这对我的要求来说是不受欢迎的。对于 IE,它运行良好。有什么方法可以提取完整参数,包括 Firefox 中的 # 符号。

please let me know if i need to post the java action code also,thanks.

如果我还需要发布 Java 操作代码,请告诉我,谢谢。

JS snippet

JS 片段

var valuePassword=test123#abcd;

    var url = "/test/ChangePwdAjax.do?newPass="+valuePassword;
            var xmlHTTP = getXMLHTTPRequest();

回答by yogi

Use

利用

var url = "/test/ChangePwdAjax.do?newPass="+ encodeURIComponent(valuePassword);

This will encode your valuePasswordto a valid URL component which can be passed as a query string in URLs

这会将您编码valuePassword为有效的 URL 组件,该组件可以作为 URL 中的查询字符串传递

And on the other side you should use decodeURIComponentto get the value from encoded string

另一方面,您应该使用decodeURIComponent从编码字符串中获取值

var value = decodeURIComponent(valuePasswordPassed);

To know more about this Go here

要了解更多关于这个去这里

回答by ZippyV

When you change data you have to do a http POST request. Not a GET request. This will automatically solve your problem without having to encode your password.

当您更改数据时,您必须执行 http POST 请求。不是 GET 请求。这将自动解决您的问题,而无需对您的密码进行编码。

xmlhttp.open("POST", "/test/ChangePwdAjax.do", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("newPass=" + valuePassword);