title: Passing html code from servlet to ajax through JSON
body: <p>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):</p>
  
  <pre><code>resultado += "&lt;a href=\"javascript:void(0);\"&gt;" + stringValue + "&lt;/a&gt;";
  </code></pre>
  
  <p>This is the way I put the html code into a JsonObject (inside my servlet) through the variable that is called "resultado":</p>
  
  <pre><code>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(); 
  </code></pre>
  
  <p>Then, the "error" attribute of $ajax() function shows me the next sentence (browser console):</p>
  
  <pre><code>SyntaxError: illegal character
  &lt;a href="\&amp;quot;javascript:void(0);\&amp;quot;"&gt;abc123...
  </code></pre>
  
  <p>This seems the html escaping is not the correct. </p>
  
  <p>jsonGeneral is a JsonObject <code>JSONObject jsonGeneral = new JSONObject();</code></p>
  
  <p>and this is my code client:</p>
  
  <pre><code>$('#cargaDocumentoForm').ajaxForm({
    dataType: 'json',
    beforeSubmit: ShowRequest,
    success: SubmitSuccesful,
    error: function (xhr, status, error){
    var err = eval("(" + xhr.responseText + ")"); 
      console.log("err: " + err.Message);        
    }
  </code></pre>