0

I need to dynamically generate some bottoms based on the return of ajax get call

@.getJSON("SearchByName", {"name":searchstring}, function(data){
    data = data.replace(/ /g, "&nbsp");
    var pop = "<p>" + data + "</p><br/><button style=\"border-radius: 
    4px;\" onclick=getDatabyName(\"" + data +"\")>Load 
    Locations</button>";
    .....

Since the return data may have space so I have to replace all spaces with &nbsp;

When users press the generated bottom to execute getDatabyName, it calls a Servlet to query a database. However, the query within the Java Servlet returns nothing because the space characters are not properly encoded. I tried to do another replace inside getDatabyName but still the same. I have ways to work around this but want to know the proper ways (either in Javascript or in Java) to handle this situation.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
user1941319
  • 375
  • 3
  • 19

1 Answers1

0

See this post on how to fix your Java Servlet to decode HTML input:

Java: How to unescape HTML character entities in Java?

PS. Change &nbsp to &nbsp; in your code.