JSON response to ajax call read as array of char

JSON response to ajax call read as array of char

SR27SR27 Posts: 3Questions: 1Answers: 0

I'm using a vb.net webmethod to return a JSON response using JsonConvert.SerializeObject from an ajax call. I'm getting a Requested unknown parameter '...' for row 0, column 0 error and then the datatable renders with the same number of rows as characters in the JSON response. The JSON is returning objects, not a simple array in the following format:

{d:[{},{}]}

My ajax does specify a "dataSrc" of "d".
How can I get it to read it as objects and not an array of characters?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923
    edited July 2018 Answer ✓

    Are you using the columns.data option to define your columns?

    If not please read the Data Source Types section of the Data manual to see how to use columns.data.

    If this doesn't help then please post your Datatables JS code and an example of your JSON response from the browser's network tools. Or use the debugger to collect this information. Post the debugger link here.

    Kevin

  • SR27SR27 Posts: 3Questions: 1Answers: 0

    Thanks. Here's a little more information. I think I am using the columns.data correctly, and I have made sure that my table columns match the number of columns in the js. I'm a bit stumped. I think the issue is with the formatting of the JSON, but I'm not sure how to output it differently.

    My html:

    <table id='tblPersonResults' class='table table-striped'>
        <thead>
          <tr>
             <th>Id</th>
              <th>First</th>
              <th>Last</th>
              <th>Other</th>
              <th>City</th>
              <th>Relationship</th>
           </tr>
          </thead>
          <tbody></tbody>
    </table>
    

    My js:

     var tblPersonResults = $('#tblPersonResults').DataTable({
                'ajax': {
                    "type": "POST",
                    "url": "my_url",
                    "dataType": 'JSON',
                    "contentType": "application/json; charset=utf-8",
                    "data": {},
                    "dataSrc": "d"
                },
                "columns": [
                    { "data": "id" },
                    { "data": "fName" },
                    { "data": "lName" },
                    { "data": "other" },
                    { "data": "address" },
                    { "data": "relationship" }
                ],
            });
    

    The JSON (output from vb.net with JsonConvert.SerializeObject method. Microsoft .net adds the "d"):

    {
       "d":[
          {
             "id":31,
             "fName":"tester",
             "lName":"testerlady",
             "other":"SSN: 1234",
             "address":"New Orleans, LA",
             "relationship":24
          },
          {
             "id":32,
             "fName":"tester",
             "lName":"testerman",
             "other":"SSN: 9876",
             "address":"New York, NY",
             "relationship":25
          }
       ]
    }
    
    
  • SR27SR27 Posts: 3Questions: 1Answers: 0

    Ok, I fixed it. The JSON was not getting processed correctly, so I had to wrap the dataSrc in a function like so:

    "dataSrc": function (json) { return $.parseJSON(json.d); }
    
This discussion has been closed.