【发布时间】:2018-03-22 16:22:23
【问题描述】:
我的 python 函数为 My Jquery GET 调用返回一个字典值。
{'Site4': {'machine': 'null', 'state': 'unprocessed'}, 'Site2': {'machine': 'null', 'state': 'unprocessed'}, 'Site3': {'machine': 'null', 'state': 'unprocessed'}, 'Site1': {'machine': 'null', 'state': 'unprocessed'}}
我想在 html 表中分别显示站点/机器/状态值。我从 My GET 调用中获取了整个字典。
我的 Jquery 函数是;如下
function loadDoc() {
var request;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
request = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e) {}
}
}
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myObject = JSON.parse(this.responseText);
alert(myObject);
document.getElementById("demo").innerHTML =
this.responseText;
}
};
request.open('GET', 'http://localhost:8080/cache/getSite?clientName=testclient', true);
request.send();
}
在上面的 JSON.parse 没有解析字典。如何在 jquery 中读取 python 字典值并在包含 3 列站点/机器/状态的表中显示?
我的 html 是;
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
<meta charset="UTF-8">
<title>Client Site Info</title>
</head>
<body>
<p id="demo"><button type="button" onclick="loadDoc()">load sites</button></p>
<table style="width:100%">
<caption>Client Site Info</caption>
<tr>
<th>Sites</th>
<th>Machines</th>
<th>ProcessingState</th>
</tr>
</table>
</body>
【问题讨论】:
-
将字典发送到带有一些变量的 html 文件,x = 你的字典。在 jquery var dic = {{x|safe}} 中将显示字典
-
@AnandThati 我想分别读取站点/机器/状态值。我从 My GET 调用中获取了整个字典。
标签: jquery python html dictionary