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

Reading with Datasource fails, Data gets scrambled

3 Answers 61 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
mgs
Top achievements
Rank 1
mgs asked on 01 Mar 2013, 11:40 AM
I read some data via Ajax as follows.

var data = {
   id: 4711,
   task: {
      name: "coding"
   }
};
var options1 = {
   type: "POST",
   dataType: "json",
   contentType: "application/json",
   data: JSON.stringify(data)
};
 
$.ajax("/api/Person/", options1);

This works perfectly. Then I try to do the same with a datasource.

var options2 = {
   transport: {
      read: {
         url: "/api/Person/",
         type: "POST",
         dataType: "json",
         contentType: "application/json",
         data: JSON.stringify(data)
      }
   }
};
 
var datasource = new kendo.data.DataSource(options2);
datasource.read();

As it fails, I also tried to remove the "JSON.stringify". However, that one also fails. Looking at the request's body with Fiddler, I see

Reading via Ajax
   {"id":4711,"task":{"name":"coding"}}
 
Reading via Datasource (with stringify)
   0=%7B&1=%22&2=i&3=d&4=%22&5=%3A&6=4&7=7& (rest stripped)
 
Reading via Datasource (without stringify)
   id=4711&task%5Bname%5D=coding

Michael G. Schneider

3 Answers, 1 is accepted

Sort by
0
mgs
Top achievements
Rank 1
answered on 04 Mar 2013, 07:46 AM
Probably the data has to be converted to JSON with the help of the parameterMap. Kendo will never convert to JSON on its own.

Is that correct?

I found out by trial and error. It was not the first thing that came to my mind, after reading the documentation. I thought that the automatic conversion to JSON would be logical, if an object was passed as "data", and if the "contentType" was set to "application/json".

Michael G. Schneider
0
Rosen
Telerik team
answered on 04 Mar 2013, 01:29 PM
Hello,

Generally speaking the DataSource is behaving the same as the jQuery.ajax.

In the jQuery code you are manually stringify the data to be send inline: 

{
   type: "POST",
   dataType: "json",
   contentType: "application/json",
   data: JSON.stringify(data)
}

Thus, there is no automatic conversion to JSON when using jQuery.ajax.

However, the difference when using our DataSource is that the additional data assigned via the data option should be an object (as it is merged with DataSource built-in options) and the parameterMap should be use to convert this new merged object to the appropriate type (in the particular case JSON):

transport: {
   read: {
      url: "/api/Person/",
      type: "POST",
      dataType: "json",
      contentType: "application/json",
      data: data
   },
   parameterMap: function(options) {
       return JSON.stringify(options);
   }
}

All the best,
Rosen
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
mgs
Top achievements
Rank 1
answered on 04 Mar 2013, 02:32 PM
Hello Rosen,

ok, I understood. Thanks a lot for the answer.

Michael G. Schneider
Tags
Data Source
Asked by
mgs
Top achievements
Rank 1
Answers by
mgs
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or