Is there a better way to access/parse my AJAX source?

Is there a better way to access/parse my AJAX source?

garuda_onegaruda_one Posts: 9Questions: 0Answers: 0
edited May 2018 in Free community support

Here's a sample of the ajax source (sorry for the sloppy indentation in these snippets):

"{"d":"[
   {
      \"ID\":2182028,
      \"Name\":\"Meetings\",
      \"ParentName\":\"Supervision\",
      \"Date\":\"2018-02-21T13:11:51.627\",
      \"Type\":1,
      \"LogID\":102602,
      \"FileType\":\".doc\",
      \"Description\":\"Could not be processed\",
      \"Comment\":\"Unsupported\"
   },
   {
      \"ID\":2182029,
      \"Name\":\"Bi-Weekly Calls\",
      \"ParentName\":\"Supervision\",
      \"Date\":\"2018-02-21T13:11:51.943\",
      \"Type\":1,
      \"LogID\":102603,
      \"FileType\":\".doc\",
      \"Description\":\"Could not be processed\",
      \"Comment\":\"Unsupported\"
   }
...
]"}"

and the DataTable initialization:

$().ready(function() {
var table = $("#Table1").DataTable({
   ajax: {
      url: "<%= ResolveUrl("~/Folder/ThisPage.aspx/GetData") %>",
      type: "GET",
      contentType: "application/json; charset=utf-8",
      dataFilter: function(dtData) {
         var p = JSON.parse(dtData);
         var d = JSON.parse(p.d);
         return JSON.stringify({ data: d });
      // dataSrc: "d" (instead of dataFilter)
   },
   columns: [
         { data: "ID", name: "id" },
         { data: "Name", name: "name" },
         { data: "ParentName", name: "parent-name" },
         { data: "Date", name:"date" },
         { data: "Type", name: "type" },
         { data: "LogID", name: "log-id" },
         { data: "FileType", name: "file-type" },
         { data: "Description", name: "description" },
         { data: "Comment", name: "comment" }
      ]
   });
});

This all works, but what I thought I could originally do was use ajax.dataSrc to the effect of dataSrc: "d". I guess I don't understand how to use that correctly though lol. Is there a better or more intuitive way than parsing twice and then re-stringifying the objects?

Replies

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945

    It looks like your server script is encoding the data into JSON then also encoding the full response into JSON. So the data portion is encoded twice which is why you have the escaped quotes (`\"). I would start by making sure you only JSON encode the response.

    Then instead of the dataFilter you can use the ajax.dataSrc as you indicate.

    Kevin

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    You are using a web method like in an asmx page? Microsoft stuff? It automatically serializes so if you are also double serializing. Some people do that on purpose because the MS serializer sucks.

  • garuda_onegaruda_one Posts: 9Questions: 0Answers: 0

    My god......I'm ashamed (yet unsurprised) at myself for not understanding why my JSON response was formatted as such. Thank you both for that insight, you were 100% correct.

This discussion has been closed.