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

19,878

Solution 1

Use

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

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

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

var value = decodeURIComponent(valuePasswordPassed);

To know more about this Go here

Solution 2

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.

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

user1697113

Updated on June 05, 2022

Comments

  • user1697113
    user1697113 almost 2 years

    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.

    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.

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

    JS snippet

    var valuePassword=test123#abcd;
    
        var url = "/test/ChangePwdAjax.do?newPass="+valuePassword;
                var xmlHTTP = getXMLHTTPRequest();
    
  • Onkelborg
    Onkelborg over 11 years
    The reason is that yo have to encode your data. A hash (#) isn't valid here, the hash, and everything that follows, should never leave your browser
  • Joachim Sauer
    Joachim Sauer over 11 years
    Personally I'd link to this documentation instead of w3schools (which have a tendency to produce sub-par documentation).
  • user1697113
    user1697113 over 11 years
    Thankyou all, it works, earlier i was limited by my thought process since in IE it was working fine.I also was wondering why both browsers were behaving differently, i agree that the URL query string must be encoded. Thanks
  • user1697113
    user1697113 over 11 years
    I tried that by changing xmlHTTP.open("GET",url,true); to xmlHTTP.open("POST",url,true); but its the same.not sure if im doing it correctly.But encoding solved my purpose.Thanks.
  • Amber McCoic
    Amber McCoic over 11 years
    @user1697113 The values shouldn't be in the url but in the body. I'll update my answer.