Unable to upgrade to 1.10.x, using JSON datasource

Unable to upgrade to 1.10.x, using JSON datasource

CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1
edited June 2016 in Free community support

I have an older DataTable that's been running fine for a couple years. I tried to clone it and upgrade to the new property names in 1.10.x. I can get the empty DataTable to display but no matter what I try, I cannot populate it with data.

I am using a c# webservice that returns a json string built via StringBuilder. It seems ugly and verbose as sin but in works in the 1.9.x world. I stripped it back so I am getting a simple json string back that looks like this:

{"d":"{\"data\": [ {\"barno\": \"012484\",\"name\": \"Lynn Eric Goar\",\"nameFirst\": \"Lynn Eric\",\"nameLast\": \"Goar\",\"companyFirm\": \"Law Office of Lynn Eric Goar PC\",\"address\": \"1955 W Grant Rd Ste 125 \",\"city\": \"Tucson\",\"state\": \"AZ\",\"zipCode\": \"85745-1481\",\"phone\": \"(520) 740-1447\",\"email\": \"lynn.goar@azbar.org\",\"registrations\": \"16100; 16101; 16102; 16103; 16116\",\"regDate\": \"1/25/2016 10:40:00 AM\"}]}"}

and my attempt to get the data:

        "ajax": {
            "url": "/services/admin/CLEbytheSea.asmx/getAllRegistrations",
            "contentType": "application/json",
            "type": "GET",
            "data": function ( d ) {
                return JSON.stringify(d);
            }
        },

What is wrong here? http://live.datatables.net/qesuyetu/1/edit

I've tried several methods of generating my json and have settled on one that creates clean json, exactly like the json that works in my legacy use of DataTables.

I just cannot get the 1.10.x version to read the json and populate the table.

This question has an accepted answers - jump to answer

Answers

  • CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1
    edited June 2016

    moved to original post

  • allanallan Posts: 62,017Questions: 1Answers: 10,166 Site admin

    The example you linked to doesn't work because there is no data.d property - data is a string. Even worse, d is a JSON encoded string inside the string! You would need to use JSON.parse( JSON.parse(data) ).d.

    Likewise, if you are getting a string in the d property of the object (I've never understood why .NET does that) you might use ajax.dataSrc like this:

    dataSrc: function ( data ) {
      return JSON.parse( data.d );
    }
    

    Allan

  • CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1

    Still throwing errors, different errors this time. I don't appear to have much control over .NET so it is what it is. I use this in my legacy version, with jQueryparseJSON, similar concept. But I cannot get anything to work in 1.10.x.

            "sAjaxSource": "/services/webmailPurchases.asmx/getWebmailPurchases",
            "fnServerData": function (sSource, aoData, fnCallback) {
                $.ajax({
                    "dataType": 'json',
                    "contentType": "application/json; charset=utf-8",
                    "type": "GET",
                    "url": sSource,
                    "data": aoData,
                    "timeout": 15000,
                    "success":
                                function (msg) {
                                    var json = jQuery.parseJSON(msg.d);
                                    fnCallback(json);
                                    $("#webmailPurchases").show();
                                },
                    "error": handleAjaxError
                });
    

    This is what I am trying to do with 1.10:

            "ajax": {
                "url": "/services/admin/CLEbytheSea.asmx/getAllRegistrations",
                "contentType": "application/json",
                "type": "GET"
            },
            dataSrc: function (data) {
                return jQuery.parseJSON(data.d);
            },
    
  • CDeCinkoCDeCinko Posts: 19Questions: 9Answers: 1
    Answer ✓

    I got it working!

            "ajax": {
                "url": "/services/admin/CLEbytheSea.asmx/getAllRegistrations",
                "contentType": "application/json",
                "type": "GET",
                "dataSrc": function (json) {
                    return JSON.parse(json.d);
                }
            },
    

    But I also have to specify the columns. For some reason columnDefs works in 1.9.x but in 1.10.x I have to use columns:

            "columns": [
                { "data": "barNo" },
                { "data": "nameFirst" },
                { "data": "nameLast" }
    ....
    
This discussion has been closed.