Question

I'm using $.ajax to get data sent to servlet but seem all data return with symbol char

var dataJson = {"title":"sss","message":"dddd","latLon":"(-30.410781790845874, 129.375)","address":"Maralinga SA 5710, Australia"} ;

$.ajax({
      url: 'http://mydomain/servler/call1',
      type: 'POST',                                     
      data:{
          data: JSON.stringify(datajson)
      },                                        
      dataType: 'json',
      success:function(results) {alert(results);
}
}); 


And my "call1" servlet

 response.setContentType("application/json");     

 BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
 String json = "";
 json = br.readLine();
 log.info("Result Data json: " + json);

===> Result Data json:  %7B%22title%22%3A%22%22%2C%22message%22%3A%22%22%2C%22latLon%22%3A%22(-41.442726377672116%2C+143.26171875)%22%2C%22address%22%3A%22Oodnadatta+SA+5734%2C+Australia%22%7D

I dont know why my web service response data with strange data....:|

Was it helpful?

Solution

When data is sent throught the request, your browser converts some character to its hexadecimal representation, escaped with a % symbol.

If you want to read it with Java, you need to decode the string you get. The package java.net.URLDecoder.decode should do the trick.

Try the following code:

response.setContentType("application/json");     

BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
json = java.net.URLDecoder.decode(br.readLine(), "UTF-8");
log.info("Result Data json: " + json);

OTHER TIPS

Thanks Antonio,

My ex:

   String fileName = "Map%20of%20All%20projects.pdf";  
   fileName = java.net.URLDecoder.decode(fileName, "UTF-8");
   System.out.print(fileName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top