我如何访问 json 响应对象字段
How do i access json response object fields
我正在尝试在响应后构建 table 并尝试将响应数据附加到 table。我的回复正确返回如下所示
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);
}
});
您的响应是单个对象,因此使用 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>
不正确。
我正在尝试在响应后构建 table 并尝试将响应数据附加到 table。我的回复正确返回如下所示
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);
}
});
您的响应是单个对象,因此使用 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>
不正确。