【发布时间】:2017-11-03 17:08:45
【问题描述】:
当一些异步任务用 celery 完成时,我会在我的 django 网站上收到 json 文件。
我想在可用时自动附加 json 数据(不刷新页面)。
有什么想法吗??
谢谢,
我尝试将 json 直接解析到表中,但它不起作用。
<table id="myTable" style="width:90%;font-style: normal;">
<thead>
<tr style="font-style: italic;">
<th><div class="th-inner"> Proc. Started </div></th>
<th><div class="th-inner"> Proc. Finished </div></th>
<th><div class="th-inner">Result </div></th>
</tr>
</thead>
<tbody>
{% for document in images %}
<tr>
<td>{{ document.image.date_image_upload }}</td>
<td> </td>
<td id=jsonResultDiv> <h3 style="color: #FFFFFF;">Results will appear here...</h3> </td>
<script type="text/javascript">
var DEV_URL = "{{ document.remote_processed_json }}";
var resultJsonUrl = DEV_URL;
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', DEV_URL, true); // Replace 'my_data' with the path to your file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
callback(xobj.responseText);
}
};
xobj.send(null);
}
loadJSON(function (response) {
// Parse JSON string into object
actual_JSON = JSON.parse(response);
var element = document.getElementById("jsonResultDiv");
element.innerHTML = "<h3>screening result:<h3>";
if (actual_JSON[0]['evaluation'] == 0)
element.innerHTML += '<h3 style="color: #66FF66;">Healthy.</h3>';
else if (actual_JSON[0]['evaluation'] == 1)
element.innerHTML += '<h3 style="color: #FF6666;">' + actual_JSON[0]['annotations'].length + ' micro found.</h3>';
else element.innerHTML += '<h3 style="color: #FFF000;">Problem parsing results file.</h3>';
});
</script>
</tr>
{% endfor %}
</tbody>
</table>
【问题讨论】:
-
你应该为此尝试一些东西。如果你卡在某个地方,请来这里询问你的代码..