我正在尝试在我的响应之后构建一个表,并尝试将响应数据附加到表中。我的回复正确,如下所示
Object {__type: "DomainLayer.BusinessLogic.UserOnlineCommunity", CommunityID: "15bb29d4-b451-4034-be7e-8197723e0a89", Description: "name", Detail: "yes", Link: "test"}
但我正在尝试访问描述或详细信息等字段,但是当我调试并将这些字段记录到控制台中时,正如我在下面的代码中所做的那样,我得到了未定义。我如何得到每个领域的价值。
function UpdateOnlineCommunity(communityId, communityName, communityDescription, communityLink) {
var serviceData = JSON.stringify({
'communityId': communityId,
'communityName': communityName,
'communityDescription': communityDescription,
'communityLink': communityLink
});
$.ajax({
type: "POST",
url: "PresentationService.asmx/UpdateUserCommunity",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: serviceData,
success: function (response) {
var data = response.d;
console.log(data);
$.each(data, function () {
var tblCommunity = $('#tblCommunity');
console.log(this['Description']);
var rowDescription = $('</tr></tr>');
rowDescription.append('<td>' + this['Description'] + '</td>');
var rowDetail = $('</tr></tr>');
rowDetail.append('<td>' + this['Detail'] + '</td>');
var rowLink = $('</tr></tr>');
rowLink.append('<td>' + this['Link'] + '</td>');
var rowUpdateBtn = $('</tr></tr>');
rowUpdateBtn.append('<button>Update</button>');
tblCommunity.append(rowDescription);
tblCommunity.append(rowDetail);
tblCommunity.append(rowLink);
});
},
error: function (response) {
alert(response);
}
});
答案 0 :(得分:0)
您的回复是单个对象,因此使用each
会导致问题。例如,您可以使用data.Description
来访问对象的属性。试试这个:
success: function (response) {
var data = response.d;
var tblCommunity = $('#tblCommunity');
var rowDescription = $('<tr />').append('<td>' + data.Description + '</td>');
var rowDetail = $('<tr />').append('<td>' + data.Detail + '</td>');
var rowLink = $('<tr />').append('<td>' + data.Link + '</td>');
var rowUpdateBtn = $('<tr />').append('<button>Update</button>');
tblCommunity.append(rowDescription, rowDetail, rowLink);
},
另请注意,您用于创建元素的语法 - </tr></tr>
不正确。