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

Trouble populating datasource from json response

2 Answers 105 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
luke
Top achievements
Rank 1
luke asked on 14 May 2013, 06:19 AM
Hi All

Not sure what i'm doing wrong exactly.

I always get an undefiend error when the fetch is called. 

Any help much appreciated

using the following code:
var dataSource = new kendo.data.DataSource({
  autoSync: true,
  transport: {
    read:  {
      url: "web url",
             beforeSend: function (xhr) {
                //your code
xhr.setRequestHeader("Authorization", "Basic AARtaW5pc3RyYXRvcjpEdWZmSVQ4OTg="); //i set the username and password to valid settings
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/json");
            },
      dataType: "json" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
        
    },
    update: {
      url: "web url",
      dataType: "json" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
    }
  },
  schema: {
    model: { id: "id" }
  }
});
dataSource.fetch(function() {
    var data = this.data();
  console.log(data.length);
//var product = dataSource.at(0);
//console.log(product.id);
});


the json returned looks like:
{

"d" : {

"results": [

{

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(1)", "etag": "W/\"4\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 1, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Duffy", "Modified": "\/Date(1367700526000)\/", "Created": "\/Date(1367696864000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(1)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(1)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 4, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(1)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Luke", "FullName": "Flukey Luke", "EmailAddress": "luke@demo.com", "Company": "COmpany Here", "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": "<div></div>"

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(2)", "etag": "W/\"5\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 2, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Sample", "Modified": "\/Date(1367700392000)\/", "Created": "\/Date(1367697021000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(2)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(2)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 5, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(2)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Sam", "FullName": null, "EmailAddress": null, "Company": "My Company", "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(3)", "etag": "W/\"2\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 3, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Tom", "Modified": "\/Date(1367700527000)\/", "Created": "\/Date(1367700507000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(3)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(3)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 2, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(3)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Testing", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": "<div></div>"

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(4)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 4, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Smith", "Modified": "\/Date(1367700554000)\/", "Created": "\/Date(1367700554000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(4)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(4)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(4)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Sam", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(5)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 5, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "man", "Modified": "\/Date(1367743184000)\/", "Created": "\/Date(1367743184000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(5)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(5)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(5)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "iron", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(6)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 6, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Using the GSON library", "Modified": "\/Date(1367793454000)\/", "Created": "\/Date(1367793454000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(6)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(6)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(6)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Using libraries is convenient.", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(7)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 7, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "Using the GSON library", "Modified": "\/Date(1367793455000)\/", "Created": "\/Date(1367793455000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(7)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(7)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(7)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Using libraries is convenient.", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(8)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 8, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "sam", "Modified": "\/Date(1367793667000)\/", "Created": "\/Date(1367793667000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(8)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(8)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(8)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Using libraries is convenient.", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}, {

"__metadata": {

"uri": "http://webadd/Samples/Enrolments(9)", "etag": "W/\"1\"", "type": "Microsoft.SharePoint.DataService.EnrolmentsItem"

}, "Id": 9, "ContentTypeID": "0x01060096C4C2ABBC5E44418CFFAEE0C90BB04A", "ContentType": "Contact", "LastName": "sam", "Modified": "\/Date(1367793667000)\/", "Created": "\/Date(1367793667000)\/", "CreatedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(9)/CreatedBy"

}

}, "CreatedById": 1, "ModifiedBy": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(9)/ModifiedBy"

}

}, "ModifiedById": 1, "Owshiddenversion": 1, "Version": "1.0", "Attachments": {

"__deferred": {

"uri": "http://webadd/Samples/Enrolments(9)/Attachments"

}

}, "Path": "/Samples/Lists/Enrolments", "FirstName": "Using libraries is convenient.", "FullName": null, "EmailAddress": null, "Company": null, "JobTitle": null, "BusinessPhone": null, "HomePhone": null, "MobileNumber": null, "FaxNumber": null, "Address": null, "City": null, "StateProvince": null, "ZIPPostalCode": null, "CountryRegion": null, "WebPage": null, "Notes": null

}

]

}

}


Thanks 

Luke

2 Answers, 1 is accepted

Sort by
0
Accepted
Atanas Korchev
Telerik team
answered on 15 May 2013, 07:03 AM
Hi,

 You need to use the schema.data option of the data source to specify which response field contains the data items. In your case it is "d.results".

All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
luke
Top achievements
Rank 1
answered on 16 May 2013, 12:36 AM
Worked a treat 

Thank you

I can read the data now fine.

However when i go to update i end up with an error

"Error processing request stream. JSON text specified is not valid."

The update code is:
   update: {
      url: "http://webservice/ListData.svc/Enrolments",
       beforeSend: function (xhr) {
                //your code
xhr.setRequestHeader("Authorization", "Basic QWRtaW5pc3RyYXRvcjpEdWZmSVQ4OTg="); //i set the username and password to valid settings
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-type", "application/json");
                               //xhr.setRequestHeader("DataServiceVersion", "1.0" );
//xhr.setRequestHeader("X-HTTP-Method", 'MERGE');
//xhr.setRequestHeader("If-Match", "*");
            },
        type: "POST",
        dataType: "json" // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
    }


Should I post this as a new question ???

Thanks again

Luke
Tags
Data Source
Asked by
luke
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
luke
Top achievements
Rank 1
Share this question
or