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

Grid + json

2 Answers 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cody
Top achievements
Rank 1
Cody asked on 07 Jun 2013, 07:46 PM
I'm having a problem getting my grid to bind to some json data. I've searched the forums for a solution but no luck.

The following code throws an error of 'Unable to get value of property 'length': object is null...':


$("#search-grid").kendoGrid({
    dataSource: {
        transport: {
            read: {
                url: "a URL"
            }
        },
        schema: {
            data: "data"
        }
    },
    columns: [{field:"Name",title:"Name"}]
});
Based on some other forum posts, this is indicative of a bad json response so i copied and pasted the json from the response to a javascript variable and it works so I'm not sure what I'm doing wrong.

This works:
var testdata = { "data": [{ "Name": "TestY" }] };
 
var searchViewModel = kendo.observable({
    init: function () {
        $("#search-grid").kendoGrid({
            dataSource: {
                data: testdata,
                /*
                transport: {
                    read: {
                        url: "a URL"
                    }
                },
                */
                schema: {
                    data: "data"
                }
                 
            },
            columns: [{field:"Name",title:"Name"}]
        });
    }



I've validated my response by other means and it is valid json so I'm not sure what to try next.  Something is "wrong" with it but I don't know what.

here is the raw response:

HTTP/1.1 200 OK
Content-Length: 27
Content-Type: application/JSON
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Fri, 07 Jun 2013 20:00:59 GMT
 
{"data":[{"Name":"TestY"}]}


Thanks in advance


Any ideas?

2 Answers, 1 is accepted

Sort by
0
Cody
Top achievements
Rank 1
answered on 07 Jun 2013, 08:18 PM
When I do this:

data: function (r) {
    alert(r);
    return r.data;
}
r is a string, not an object. I believe it is supposed to be an object if it can be parsed. Based on the documentation.

schema: {
  data: function(response) {
    return response.results; // twitter's response is { "results": [ /* results */ ] }
  }
So something is wrong with my json or my call to the service that is preventing it from being parsed.  
0
Accepted
Cody
Top achievements
Rank 1
answered on 07 Jun 2013, 08:21 PM
Figured it out. You have to add the datatype to the read object. Its in the docs, I just overlooked it.

transport: {
    read: {
        url: "a URL",
        dataType: "json"
    }
},
Tags
Grid
Asked by
Cody
Top achievements
Rank 1
Answers by
Cody
Top achievements
Rank 1
Share this question
or