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

2022-01-15 00:00:00 firefox xmlhttprequest javascript ajax

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

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.

例如,如果我在 Firefox 中通过了 test123#abcd,那么我在动作类中只得到 test123,而不是 test123#abcd,这对我的要求来说是不可取的.对于 IE,它工作得很好.有什么办法可以我可以提取完整的参数,包括 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.

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

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

JS 片段

var valuePassword=test123#abcd;

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

推荐答案

使用

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

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

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

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

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

var value = decodeURIComponent(valuePasswordPassed);

要了解更多信息去这里

相关文章