在jQuery AJAX成功回调中解析JSON列表(通过C#)

问题描述:

我正在使用jQuery的AJAX方法来调用C#服务以返回JSON序列化列表.

I'm using jQuery's AJAX methods to call a C# service to return a JSON serialized list.

[HttpPost]
public JsonResult SearchTicket(ViewModelTicket ticket) {

    var list = UnitOfTicket.Where(x =>x.TicketId == ticket.TicketId);

    return Json(new { list  }, JsonRequestBehavior.AllowGet);
}

我从成功回调函数中解析响应,并将其呈现为HTML.

I parse the response from within the success callback function and render it as HTML.

 $.ajax({
        type: "POST",
        url: url,
        data: JSON.stringify(Ticket),
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        success: function (list) {
            var data = list;
            for (var i in data) {
                alert(JSON.stringify(data[i]));
                $('#tbody-element').append(
                     '<tr>' +
                         '<td>' + data[i].TicketId + '</td>' +
                         '<td>' + data[i].Title + '</td>' +
                         '<td>' + data[i].PriorityId + '</tr>' +
                         '<td>' + data[i].OpenDateAndTime + '</tr>' +
                         '<td>' + data[i].SlaExpiration + '</td>' +
                     '</tr>'
                 );
             }
         },
         error: function () {
             alert("Error occured!!")
         }
     });

响应显示在警报中

[{"TicketId":1,"OpenDateAndTime":"/Date(1517833557277)/","ClosedDateTime":null,"VersionId":140,"PriorityId":2,"CompanyId":0,"UserId":null,"Rate":null,"SlaExpiration":null,"TicketTypeId":1,"StatusId":1,"ProductId":1,"SubProductId":1,"TaskId":1,"Title":"Primeiro Chamado","Files":null}]

我的问题是用未定义的值渲染对象.例如:data [i] .Title ...

My problem is rendering an object with an undefinded value. For example: data[i].Title ...

我正在关注该帖子:在以下位置解析返回的C#列表AJAX成功功能

如果您尝试alert(JSON.stringify(data)),您会看到什么?您的代码就像data是对象数组一样工作,但实际上它可能是充满嵌套数组的数组.

If you try alert(JSON.stringify(data)) what do you see? Your code works as if data is an Array of objects, but it may actually be an Array full of nested Arrays.

如果alert(JSON.stringify(data[i]));向您显示[{"TicketId":...}],则看起来data[i]是一个包含单个对象的数组-在这种情况下,data[i].TicketId是未定义的,但data[i][0].TicketId应该具有所需的值.在这种情况下,全面使用data[i][0]代替data[i]应该会帮助您.

If alert(JSON.stringify(data[i])); is showing you [{"TicketId":...}] it looks like data[i] is an Array containing a single object--in this case, data[i].TicketId is undefined but data[i][0].TicketId should have the value you want. If this is the case, using data[i][0] instead of data[i] across the board should help you out.

注意:不过,我会谨慎使用for (var i in data),因为如果dataArray,并且您向Array.prototype添加了任何类型的数据(例如,polyfilled Array方法),则它会将包含在此循环中.使用起来更安全:

Note: I'd be careful about using for (var i in data), though, since if data is an Array and you have any sort of data added to Array.prototype (e.g. a polyfilled Array method), then it will be included in this loop. It'd be safer to use:

for (var i in data) {
    if (data.hasOwnProperty(i)) {
        ...
    }
}