我正在从 Web 服务获取 json 字符串,并希望在 html 页面中显示它。我使用 ajax 来动态显示表格中的行。但问题是所有记录都存在于单行中。
代码:
function showall(){
var user = document.getElementById("user_name").value;
alert(user);
var row = $("<tr>");
var nextrow = $("</tr>");
$.ajax({
type: "POST",
url: "webresources/hello/showrequests",
data : user,
success : function(data){
console.log(data);
$.each(data, function(index, store){
$.each(store, function(key,value){
row.append($("<td>").html(value));
});
$('#requests').append(row);
$('#requests').append(nextrow);
});
},
error: function(e){
alert("error"+e+"has occured");
}
});
}
和 html 正文
<body onload="showall();">
<h1><%=user%></h1>
<input type="hidden" value="<%=user%>" id="user_name">
<table border="1" id="requests" style="border-collapse: collapse">
<tr>
<th>RequestId</th>
<th>Request</th>
<th>Category</th>
<th>Subcategory</th>
<th>Department</th>
<th>Status</th>
</tr>
</table>
</body>
输出就像
col11 | col12 | col13 | col21 | col22 | col23|
我期待像
col11 | col12 | col13 |
col21 | col22 | col23|
我从网络服务发送的 JSONArray 就像
[{"requestid":1,"request":"Increase RAM from 4GB to 8GB","category":"Hardware","subcategory":"To increase the RAM","department":"System","status":"Pending for approval"},{"requestid":2,"request":"Increase RAM from 2GB to 4GB\n","category":"Hardware","subcategory":"To increase the RAM","department":"System","status":"Pending for approval"},{"requestid":3,"request":"Increase RAM from 1 GB to 4 GB","category":"Hardware","subcategory":"To increase the RAM","department":"System","status":"Pending for approval"}]
我还需要在 ajax 中做什么?谢谢