This is a migrated thread and some comments may be shown as answers.

JSON issue driving me crazy

2 Answers 139 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Gabriel
Top achievements
Rank 1
Gabriel asked on 12 Jan 2012, 05:07 AM
I'm having issues with a Datasouce when using a JSON service/data. I constantly get an error telling me that any of my fields are undefined. According to Fiddler, result is returned as JSON and is correctly formatted. This the same issue I'm having here: http://www.kendoui.com/forums/ui/autocomplete/weird-problem-with-autocomplete.aspx

As per the above post, the solution works fine in jsFiddle but NOT in the real world.

I'm creating my grid like this:
$("#gridTitles").kendoGrid({
            dataSource: {
                type: "json",
                transport: {
                    read: "../services/getTitles",
                    parameterMap: function (options) {
                        return $.extend(options, {
                            collection: collection
                        });
                    }
                },
                schema: {
                    model: {
                        fields: {
                            Title: { type: "string" },
                            OFLC: { type: "string" }
                        }
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverSorting: true
            },
            height: 250,
            sortable: true,
            pageable: true,
            columns: [{ field: "Title", title: "Title" }, { field: "OFLC", title: "Rating"}]
 
        });

Here is the data returned:
[{"Title":"Doctor Who and the Daleks","OFLC":"PG"},{"Title":"Doctor Who: Attack of the Cybermen","OFLC":"M"},{"Title":"Doctor Who: Battlefield","OFLC":"PG"},{"Title":"Doctor Who: Beneath the Surface - The Silurian Collection","OFLC":"PG"},{"Title":"Doctor Who: Black Orchid","OFLC":"PG"}]

I'm just guessing it is because the data isn't layed out like this:
{["SOME KEY": [{"Title": "something here...", "oflc":"something here..."}]
}

I could be wrong, but looking around these forums, everone who seems to have the 'undefined' issue on fields seem to have their JSON similar to how mine is returned. I have another datasource with data in the directly above format returned from a service that works...

Does the data returned need to be parsed or stringified before use? I'm using MS's JSON formattes which encode data and return as a string. This is being written directly to the response stream. Strange as Fiddler can understand it just fine.

2 Answers, 1 is accepted

Sort by
0
Accepted
Andrew
Top achievements
Rank 1
answered on 12 Jan 2012, 05:45 AM
For the grid, I found you did need to format the JSON similar to what  you are thinking - besides you'll want to add a TOTALS attribute if you want to do any paging anyways.   Here is a sample I am using (formatted for clarity).  Also,  your schema definition needs to add mappings for which variable in the JSON holds the grid data (in my example, i would add data: "organization_recs", totals : "totals")

{"organization_recs":
[
  {"organization_id":1,"organization_name":"Company One"},
  {"organization_id":2,"organization_name":"Company Two"},
  {"organization_id":3,"organization_name":"Company Three"}
],"totals":3}
0
Gabriel
Top achievements
Rank 1
answered on 12 Jan 2012, 09:47 PM
Thanks Andrew.
Tags
Data Source
Asked by
Gabriel
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Gabriel
Top achievements
Rank 1
Share this question
or