Search code examples
javascripthtmljsonparsingjsonp

parse html code from json using native javascript


Is it possible to get out the code that is inside a json file? You can have a look at it here: http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4 What is the code or method should I use to put the html code inside any raw div? I tried to use jsonp, but there is a mistake:

unexpected token <

<script>
    function myFunction(data){              
        var arr = JSON.parse(data);
        document.getElementById('advBlock').innerHTML = arr;
    }
</script>
<script type="text/javascript" src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>

Solution

  • The JSONP data is already a Javascript object, not a JSON string, so you don't need to parse it:

    <div id="advBlock"></div>
    <script>
      function myFunction(data) {
        document.getElementById('advBlock').innerHTML = data;
      }
    </script>
    <script src="http://www.bryzgalov.directadvert.ru/show.cgi?adp=768&json=4&callback=myFunction"></script>