【发布时间】:2016-02-01 12:40:10
【问题描述】:
我正在尝试在 HTML5 页面中绑定来自 WebAPI 调用的 json 响应。不知道我在哪里。 WebAPI 返回这个 json:
{
"ID": 1,
"Date": "2015-10-26T00:00:00",
"Status": "Initiated",
"Action": {
"VerificationActionTypeID": 0,
"VerificationActionType": null,
"VerificationActionTakenID": 0,
"VerificationActionTaken": null,
"VerficationActionCreateDate": "0001-01-01T00:00:00",
"EmailAddress": null,
"Notes": null
},
"Actions": [
{
"VerificationActionTypeID": 0,
"VerificationActionType": "Perform Rinse Flowcell",
"VerificationActionTakenID": 0,
"VerificationActionTaken": "Skip",
"VerficationActionCreateDate": "2015-10-26T10:04:05.093",
"EmailAddress": null,
"Notes": null
}
]
}
这是我的 jQuery 代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
var uri = 'http://localhost/Custom.WebAPI/api/action?verificationid=1';
var $result = $('#Result');
var msg;
$(document).ready(function () {
$.getJSON(uri)
.done(function (data) {
$.each(data, function (key, item) {
alert("action: " + item);
$('#actions tbody').append('<tr><td>' + item.ID + '</td><td>' + item.Status + '</td></tr>');
});
})
.fail(function (jqXHR, textStatus, err) {
var error = $.parseJSON(jqXHR.responseText);
msg = "Failed to get action data Error message is " + error.message;
alert("Message: " + msg);
});
});
</script>
我的 HTML:
<body>
<aside id="data">
<div id="Result"> </div>
<table id="actions">
<thead>
<tr>
<td>ID</td>
<td>Status</td>
</tr>
</thead>
<tbody></tbody>
</table>
</aside>
</body>
这是在 html 页面上显示的内容:
请注意我在 javascript 中的这一行:
alert("action:" + item);
它显示这样的警报消息:
我只是想在 html 表格中显示结果。不知道我做错了什么。感谢您提供的任何帮助。
【问题讨论】:
标签: jquery html asp.net-web-api