How do I get Mithril.js v0.2.5 to render raw HTML extracted from json?

时间:2016-10-19 13:36:16

标签: html json mithril.js

I created a cgi script that outputs:

{"table_1":"<table><tr><td>one</td><td>1</td></tr><tr><td>two</td><td>2</td></table>"}

Mithril.js can fetch this JSON and put it into a div, but the HTML is not rendered. How do I safely get mithril to process the html before displaying it?

<!doctype html><html><head><meta charset='utf-8'>
<script src="mithril.min.js"></script><title>Mithril simple JSON</title></head>
<body>
  <div id="cgi"><pre>
#!/bin/bash
# example_json.cgi

echo Content-type: application/json
echo
echo '{"table_1":"<table><tr><td>one</td><td>1</td></tr><tr><td>two</td><td>2</td></table>"}'
  </pre></div>

  <div id="live"/>

  <script>  
  var mod = { 
    controller : function() {
        this.test = m.request({
            method : "GET",
            url : "/cgi-bin/example_json.cgi",
        })
        .then(function (data) {
            return data["table_1"];
        });
    },
    view : function(ctrl) {
        console.log(ctrl.test());
        return ctrl.test() ? ctrl.test() : 'Loading';
    }
 };
 m.module(document.getElementById("live"), {controller: mod.controller, view: mod.view});
 </script>
</body></html>

0 个答案:

没有答案
相关问题