0

I want to put some html code to a JSONObject inside a Java servlet and send to the client using ajax, this is my html code that I want to put into a string variable called "resultado" (escaping html characters):

resultado += "<a href=\"javascript:void(0);\">" + stringValue + "</a>";

This is the way I put the html code into a JsonObject (inside my servlet) through the variable that is called "resultado":

response.setContentType("text/html; charset=windows-1252");
PrintWriter out = response.getWriter();
jsonGeneral.put("error", 0);
jsonGeneral.put("contenido", resultado);
out.print(jsonGeneral.toString());
out.close(); 

Then, the "error" attribute of $ajax() function shows me the next sentence (browser console):

SyntaxError: illegal character
<a href="\&quot;javascript:void(0);\&quot;">abc123...

This seems the html escaping is not the correct.

jsonGeneral is a JsonObject JSONObject jsonGeneral = new JSONObject();

and this is my code client:

$('#cargaDocumentoForm').ajaxForm({
  dataType: 'json',
  beforeSubmit: ShowRequest,
  success: SubmitSuccesful,
  error: function (xhr, status, error){
  var err = eval("(" + xhr.responseText + ")"); 
    console.log("err: " + err.Message);        
  }
jcflorezr
  • 326
  • 4
  • 13
  • Why are you lying about your `Content-Type`? – SLaks Feb 05 '14 at 16:09
  • @SLaks Where is he doing that? – mattbdean Feb 05 '14 at 16:11
  • @whowantsakookie: `setContentType("text/html` – SLaks Feb 05 '14 at 16:11
  • Also, what is `jsonGeneral`, and what is your client code? – SLaks Feb 05 '14 at 16:12
  • @SLaks Oh I thought you were talking about User-Agent for some reason. My bad. – mattbdean Feb 05 '14 at 16:12
  • About the content type in my servlet I tried with "application/json; charset=utf-8", application/x-www-form-urlencoded; charset=UTF-8" but It doesn't work with none of them – jcflorezr Feb 05 '14 at 16:22
  • I got it! All I needed was just add the html code to a wrapper tag that I create in response time: var message = $("

    ").html(responseText.contenido).text(); $('#estado_upload').html(message); Where "message" is a var where I get the response from the servlet (my JSON including the html code specified in my question) wrapped in a `

    ` tag, it could be any tag (`
    `, `
    `, however).
    – jcflorezr Feb 05 '14 at 20:11

1 Answers1

0

When I have needed to pass XML or similar in the past, we used base-64 or URL encoding on the string to avoid any need to worry about character escaping.

From a Java point of view, the following may be of use (there is too much code and too varying a view point to make it worth me picking one, as someone will complain): Decode Base64 data in Java

Ajax I've not looked into but there will be examples around somewhere.

Community
  • 1
  • 1
Paul Eden
  • 338
  • 1
  • 3
  • 10
  • Could you share with me some code where I could use base-64 or url encoding? – jcflorezr Feb 05 '14 at 16:37
  • Edited to include a link – Paul Eden Feb 05 '14 at 16:58
  • I got it! All I needed was just add the html code to a wrapper tag that I create in response time: var message = $("

    ").html(responseText.contenido).text(); $('#estado_upload').html(message); Where "message" is a var where I get the response from the servlet (my JSON including the html code specified in my question) wrapped in a `

    ` tag, it could be any tag (`
    `, `
    `, however).
    – jcflorezr Feb 05 '14 at 20:11