Reading with Datasource fails, Data gets scrambled

4 posts, 0 answers
  1. mgs
    mgs avatar
    163 posts
    Member since:
    Jan 2011

    Posted 01 Mar 2013 Link to this post

    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
  2. mgs
    mgs avatar
    163 posts
    Member since:
    Jan 2011

    Posted 04 Mar 2013 Link to this post

    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
  3. Kendo UI is VS 2017 Ready
  4. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 04 Mar 2013 Link to this post

    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!
  5. mgs
    mgs avatar
    163 posts
    Member since:
    Jan 2011

    Posted 04 Mar 2013 Link to this post

    Hello Rosen,

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

    Michael G. Schneider
Back to Top
Kendo UI is VS 2017 Ready