Pass Date to Amsx Through Datasource

5 posts, 0 answers
  1. William
    William avatar
    23 posts
    Member since:
    Jan 2012

    Posted 14 Feb 2012 Link to this post

    I am working with the datasource control and I am trying to query a webservice and need to pass a start date from a Kendo datepicker.  When it is sent I get an error - Invalid JSON Primitive.  Any help would be appreciated, thanks. Here is my code:
    var dataSource = new kendo.data.DataSource({  
     transport: {
      read: {
       url: '/app/handlers/customers.asmx/GetTopAssignedCustomers',
       dataType: "json",
       data: startDate: function(){return kendo.toString(new Date(start.value()), "MM/dd/yyyy");},
       contentType: 'application/json; charset=utf-8',
       type:"POST"     
       }
     },
     schema: {
      data:"d"
     }
    });

  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 15 Feb 2012 Link to this post

    Hi William,

    You are receiving the error, because the web service expects date/time format in form of "\/Date(x)\/ ", where x is the number of ms elapsed since Jan. 1, 1970. To solve the problem you have to convert JavaScript dates into the required format before sending them to the web server.
    So instead of:
    function(){return kendo.toString(new Date(start.value()), "MM/dd/yyyy");}
    Try using:
    function() {
        var converted = '"\\\/Date(' + start.value().getTime() + ')\\\/"';
        return converted;
    }

    Please let me know if you have any further questions about this problem.

    Greetings,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. William
    William avatar
    23 posts
    Member since:
    Jan 2012

    Posted 15 Feb 2012 Link to this post

    Alexander,

    I still get the error.  Here is the text view from Fiddler -

    startdate=%22%5C%2FDate(1329282000000)%5C%2F%22&enddate=%22%5C%2FDate(1329282000000)%5C%2F%22&associate=ALL&count=25&storeID=100.

    At this point I am assuming the issue is that this data is not being sent correctly as JSON.  Here is my datasource code:

    var dataSource = new kendo.data.DataSource({  
        transport: {
            read: {
                url: '/app/handlers/customers.asmx/GetTopAssignedCustomers',
                dataType: "json",
                data:{
                    startdate: function(){return ConvertDate(start.value());},
                    enddate: function(){return ConvertDate(end.value());},
                    associate: function(){return $("#AssociateSelect").val();},
                    count: function(){return $("input[name=Count]:checked").val();},
                    storeID:"<%=StoreID%>"
                    },
                contentType: 'application/json; charset=utf-8',
                type:"POST"                    
                }
        },
        schema: {
            data:"d"
        }
    });
  5. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2877 posts

    Posted 16 Feb 2012 Link to this post

    Hi William,

    jQuery does not send the data as JSON by default. Try using JSON.stringify to convert JavaScript data structure into JSON text. You can find more information about that issue here.

    Please check the code of that demo, there is an example using stringify method.

    Regards,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. William
    William avatar
    23 posts
    Member since:
    Jan 2012

    Posted 17 Feb 2012 Link to this post

    Ok, thanks.

    Wade
Back to Top
Kendo UI is VS 2017 Ready