简体   繁体   English

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

[英]Parse JSON List (via C#) in jQuery AJAX Success Callback

I'm using jQuery's AJAX methods to call a C# service to return a JSON serialized list. 我正在使用jQuery的AJAX方法来调用C#服务以返回JSON序列化列表。

[HttpPost]
public JsonResult SearchTicket(ViewModelTicket ticket) {

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

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

I parse the response from within the success callback function and render it as HTML. 我从成功回调函数中解析响应,并将其呈现为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!!") } }); 

The response is displayed in an alert: 响应显示在警报中:

[{"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}]

My problem is rendering an object with an undefinded value. 我的问题是渲染具有未定义值的对象。 For example: data[i].Title ... 例如:data [i] .Title ...

I am following this post: Parse returned C# list in AJAX success function 我正在关注此帖子: 在AJAX成功函数中解析返回的C#列表

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

Your response is already parsed which means your list variable is an array. 您的响应已被解析,这意味着您的list变量是一个数组。

If you try alert(JSON.stringify(data)) what do you see? 如果尝试使用alert(JSON.stringify(data)) ,您会看到什么? Your code works as if data is an Array of objects, but it may actually be an Array full of nested Arrays. 您的代码就像data是对象数组一样工作,但实际上它可能是充满嵌套数组的数组。

If alert(JSON.stringify(data[i])); 如果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. 正在向您显示[{"TicketId":...}] ,看起来data[i]是一个包含单个对象的数组-在这种情况下, data[i].TicketId是未定义的,但data[i][0].TicketId应该具有所需的值。 If this is the case, using data[i][0] instead of data[i] across the board should help you out. 在这种情况下,全面使用data[i][0]而不是data[i]应该可以帮助您。


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 (eg a polyfilled Array method), then it will be included in this loop. 注意:不过,我会谨慎使用for (var i in data) ,因为如果data是一个Array并且您向Array.prototype添加了任何类型的数据(例如,polyfilled Array方法),那么它将包括在内在这个循环中。 It'd be safer to use: 使用起来更安全:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM