简体   繁体   中英

How to parse json Data and display it using Ajax in JQuery?

There is an ajax call to system which returns JSOn as response and display it in HTML table.

In HTML we have following Code where data is to be displayed:

<table id="documentDisplay">
        <thead>
          <tr>
              <th align="left">Document Type</th>
            <th align="left">File Name</th>
            <th align="left">Document Date</th>
          </tr>
        </thead>
      </table>

Following is Ajax call and corresponding method call:

documentCommon.ajax({
        blockUI: false,
        type: 'POST',
        //dataType: 'html',
        url : 'loaddocument.json',
        data: {
            '_CONV_ID': $('input[name="_CONV_ID"]').val(),
        },
        success: function (data) {
            alert(data);
            console.log(data);

            document.documentDisplay.clear().rows.add(data.lines).draw();
            //Here we get an error  
        },

    });



document.documentDisplay= $('#documentDisplay').DataTable($.extend({}, documentCommon.tableTemplate, {
            asStripClasses: [], // No stripes
            width:     "100%",
            scrollY: "150px",
            columnDefs: [
                {"width": "20%", targets: 0},
                {"width": "40%", targets: 1},
                {"width": "40%", targets: 2},
            ],
            columns: [
                {"data": "documentType"},
                {"data": "fileName"},
                {"data": "documentDate"}
            ]
    }));

Ajax is able to send request and bring back the data. Following is firebug output:

[
{
  "documentDate":[
     2015,
     8,
     18,
     13,
     1,
     51
  ],
  "documentType":"X",
  "documentId":18038,
  "fileExtension":"pdf",
  "filename":"kuka.pdf",
  "link":"http://something.com/kuka.pdf",
},
So on and end with ]

Following is error which is displayed in Firebug:

TypeError: a is undefined

Is there something wrong in code? Is there some other way to implement the same? Also, i want check on documentType in datatables ie if documetType is X then display else do not display. Is there any way to do the same?

如果您使用的是jQuery AJAX,则可以通过以下代码直接获取JSON数据: data.responseJSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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