1

I am retrieving the body element of a JSON file, which is actually html code. I am trying to then add it to my HTML, but instead of running the code it prints as a p or header element(I suppose). Can any please tell me what I am doing wrong I am unable to find answers on other discussions.

<div id="title">

</div>


<div id="body">

</div>

<script>
  var requestURL = 'https://secure.toronto.ca/cc_sr_v1/data/swm_waste_wizard_APR?limit=1000';
  var request = new XMLHttpRequest();
  request.open('GET', requestURL);
  request.responseType = 'json';
  request.send();
  request.onload = function() {
    var data = request.response;
    document.getElementById("title").innerHTML = data[0].title;
    document.getElementById("body").innerHTML = data[0].body;
  }
</script>

2 Answers2

3

You can use a DOMParser to parse your string with HTML entities to a string of HTML which can then be rendered as a DOM element.

document.getElementById("body").innerHTML = htmlDecode("&lt;ul&gt; \n &lt;li&gt;Place item in the &lt;strong&gt;Garbage Bin.&lt;/strong&gt;&lt;/li&gt; \n&lt;/ul&gt;");

function htmlDecode(input) {
  var doc = new DOMParser().parseFromString(input, "text/html");
  return doc.documentElement.textContent;
}
<div id="body"></div>

You can use this like so:

<div id="title"></div>
<div id="body"></div>

<script>

  function htmlDecode(input) {
    var doc = new DOMParser().parseFromString(input, "text/html");
    return doc.documentElement.textContent;
  }

  var requestURL = 'https://secure.toronto.ca/cc_sr_v1/data/swm_waste_wizard_APR?limit=1000';
  var request = new XMLHttpRequest();
  request.open('GET', requestURL);
  request.responseType = 'json';
  request.send();
  request.onload = function() {
    var data = request.response;
    document.getElementById("title").innerHTML = htmlDecode(data[0].title);
    document.getElementById("body").innerHTML = htmlDecode(data[0].body);
  }
</script>

Credit to this answer for DOMParser.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

I have used Jquery's "unescape" function. You can read it at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape

The unescape() function computes a new string in which hexadecimal escape sequences are replaced with the character that it represents. The escape sequences might be introduced by a function like escape. Usually, decodeURI or decodeURIComponent are preferred over unescape.

Please try this, hope this helps. Thnx

var requestURL = 'https://secure.toronto.ca/cc_sr_v1/data/swm_waste_wizard_APR?limit=1000';
  var request = new XMLHttpRequest();
  request.open('GET', requestURL);
  request.responseType = 'json';
  request.send();
  request.onload = function() {
    var data = request.response;
    var title_ = $("<p/>").html(data[0].title).text(); 
    var body_ = $("<p/>").html(data[0].body).text(); 
    //$('#body').append($.parseHTML(unescape(data[0].body)));
    $('#title').append(title_);
    $('#body').append(body_);
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="title">

</div>

<div id="body">

</div>

You have to append/prepend the data[i] to the elements. Not set the value (like ht)

Kumar Ashutosh
  • 1,121
  • 10
  • 33
  • Thanks alot that worked. Can you explain to me why my solution wasnt working and do you have a pure javascript solution – iceicebabeh Jan 18 '19 at 02:17
  • I'm not that much into pure native javascript. But, yes there might be another solution for this in native. Your data[0].title and others were returning utf encoded string, (tag travel in network in encoded format) so, when you receive htem, you might want to convert them back into their original form. That's where escape/encode/un-escape/decode come in picture. – Kumar Ashutosh Jan 18 '19 at 02:35