Read Can't get Right Json from Server

9 posts, 0 answers
  1. ivan
    ivan avatar
    27 posts
    Member since:
    Aug 2013

    Posted 17 Nov 2014 Link to this post

    I found a very strange problem, and it took my another 4 hours, can't solve it:

    BackGround: the latest kendoUI. the scheduler could show it's self, with no js errors. The client-side code is below:

    jQuery("#workcalendar").kendoScheduler({
            date: new Date(),
        startTime: new Date("2013/6/13 07:00 AM"),
        height: 600,
        views: [
            "day",
            { type: "workWeek", selected: true },
            "week",
            "month",
            "agenda"
        ],
        //timezone: "Etc/UTC",
            dataSource:
            {
                  transport:
                  {
                    read:
                    {
                        type: "POST",
                        url: "KPIGetData/DimData.aspx?SearchInput=schedulerevent",
                        dataType: "json"
                         
                    },
                    update:
                    {
                        type: "POST",
                        url: "KPIGetData/DimSavingData.aspx?SearchInput=schedulereventedit",
                        dataType: "json"
                    },
                    create:
                    {
                        type: "POST",
                        url: "KPIGetData/DimData.aspx?SearchInput=schedulereventadd",
                        dataType: "json"
                    },
                    destroy:
                    {
                        type: "POST",
                        url: "KPIGetData/DimData.aspx?SearchInput=schedulereventdel",
                        dataType: "json"
                    }
                     ,
                         parameterMap: function(options, operation)
                         {
                          if (operation === "read")
                          {
                                var result ="&SearchValue="+CurrentUserID+"&StartTime="+SearchTimeStart+"&EndTime"+SearchTimeEnd;
                                alert("hit:"+result);
                                return kendo.stringify(result);
                            }
                            else if (operation !== "read" && options.models)
                            {
                                return {models: kendo.stringify(options.models)};
                            }
                         }
                  }
                  ,
                  schema:
                  {
                    model:
                    {
                      id: "taskId",
                      fields:
                      {
                        taskId: { from: "TaskID",type: "number"},
                        title: { from: "Title",defaultValue: "No title", validation: { required: true }},
                        start: { type: "date", from: "Start" },
                        end: { type: "date", from: "End" },
                        description: { from: "Description" },
                        recurrenceId: { from: "RecurrenceID" },
                        recurrenceRule: { from: "RecurrenceRule" },
                        recurrenceException: {from:"RecurrenceException"},
                        ownerId: {from: "OwnerID", defaultValue: 1},
                        isAllDay: {type: "boolean",from:"IsAllDay"}
                      }
                    }
                  },
                   requestEnd: function(e)
                   {
                    var response = e.response;
                    alert("here:"+response +"|"+JSON.stringify(response));
                     
                     
                  },
                   error: function(e) {
                    alert(e.status);
                   }
            }
           
        });

    On the server side, it's asp.net c#. And I have  an A/B test.

    Test A:  
    string test1 = "[{\"TaskID123\":\"885\"}]";
      Response.Clear();
      Response.ContentType = "application/json";
      Response.Charset = "UTF-8";
      Response.Write(test1);
      Response.End();
    In Test A, I just write a json string into an variable, then response back it to the browser.  In the requestEnd: I received it correctlly. Please NOTE that I know it's not a full set of the fields, my point is not talking about that, just see the Test B.

    Test B:
      DataTable dt = null;
      DataRow[] drdtv = null;
      using (System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["HROK"].ConnectionString))
      {
                    cn.Open();
                   string sql = "select top 1 TableID from OAOrderTaskFactBill";
                    SqlCommand cmd = new SqlCommand(sql, cn);
                   SqlClient.SqlDataAdapter ada =new SqlClient.SqlDataAdapter();
                    ada.SelectCommand = cmd;
                   ada.Fill(dt);
    }
     
      string test2 = "[{" + "\"" + "TaskID" + "\"" + ":" + "\"" + dt.Rows[0]["TableID"].ToString() + "\"" + "}]";
     
     Response.Clear();
     Response.ContentType = "application/json";
    Response.Charset = "UTF-8";
    Response.Write(test2);
    Response.End();

    Please NOTE that the C# has no errors, and the server could return the json VERY WELL.  I know it because I copy the "KPIGetData/DimData.aspx?SearchInput=schedulerevent/&SearchValue="+CurrentUserID+"&StartTime="+SearchTimeStart+"&EndTime"+SearchTimeEnd" in to the brower address-line and enter to see the return json. In TestA and TestB, both return [{"TaskID123":"885"}]

    But in TestB, the Scheduler can't receive the json in the requestEnd: at all! 
    alert("here:"+response +"|"+JSON.stringify(response));
    it shows:  here:undefined|undefined

    I test these for many times, the result is the same.
    I found that the ONLY diffrent between A/B is, in TestA, the Json is in a constant, hard coded string like 885, even you hard code it into an variable, it's a constant in fact. But in TestB, the Json is created from a real variable, which get value from database.

    So the conclusion is, (I know it's impossible, but don't know what's wrong), the Scheduler's Read could only get Json from a hard coded constant string, may be your demo site use the hard coded constant string Json to demo in your backend?  When the Json is not hard coded in the C#, Scheduler will get an undefined or just an empty.

    By the way, I have tested this:
     return new JavaScriptSerializer().Serialize(Tasktings);
    Taskting is a list<> which created to hold the values.
    The result is same.

    I swear I met this problem( a hard coded json in c#, is diffrent with one created from variable) many years ago. But I did not write down what happened and how to solve it.

    Please help, the scheduler is very hard to use, document and online demo has litte full-example, only show one of the few main functions.
    I have spend over 15 hours in the sheduler,  I am very sorry, but please help.

    yours,
    Ivan








  2. ivan
    ivan avatar
    27 posts
    Member since:
    Aug 2013

    Posted 19 Nov 2014 Link to this post

    i cant get this problem out. try all hard. help please!
  3. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 19 Nov 2014 Link to this post

    Hi Ivan,

    From the provided information it seems that the response from the server in second case is not recognized as valid JSON. As the dataSource internally use jQuery "ajax" method for creating requests to the server - could you please test the same case using only jQuery "ajax" method (without Kendo UI) and let us know of the result? If the same behavior is reproduced (the "error" event of the "ajax" request is triggered) I would suggest to inspect more carefully the response from the server using your browser (dev tools) "network tab" - if the response is valid in both cases.

    Regards,
    Vladimir Iliev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  4. ivan
    ivan avatar
    27 posts
    Member since:
    Aug 2013

    Posted 20 Nov 2014 in reply to Vladimir Iliev Link to this post

    Thank you, Iliev, very interesting.
    I tested it as you told, but result is  not as you thought.

    the code:
            jQuery("#workcalendar").kendoScheduler({
                date: new Date(),
            startTime: new Date("2013/6/13 07:00 AM"),
            height: 600,
            views: [
                "day",
                { type: "workWeek", selected: true },
                "week",
                "month",
                "agenda"
            ],
            //timezone: "Etc/UTC",
                dataSource:
                {
                      transport:
                      {
                        read:
                        {
                            type: "POST",
                            url: "KPIGetData/DimData.aspx?SearchInput=schedulerevent",
                            dataType: "json"
                             
                        },
                        update:
                        {
                            type: "POST",
                            url: "KPIGetData/DimSavingData.aspx?SearchInput=schedulereventedit",
                            dataType: "json"
                        },
                        create:
                        {
                            type: "POST",
                            url: "KPIGetData/DimData.aspx?SearchInput=schedulereventadd",
                            dataType: "json"
                        },
                        destroy:
                        {
                            type: "POST",
                            url: "KPIGetData/DimData.aspx?SearchInput=schedulereventdel",
                            dataType: "json"
                        }
                         ,
                             parameterMap: function(options, operation)
                             {
                              if (operation === "read")
                              {
                                    //var result ="&SearchValue="+CurrentUserID+"&StartTime="+SearchTimeStart+"&EndTime="+SearchTimeEnd;
                                    var result ="&SearchValue=475&StartTime=2014/1/1&EndTime=2015/1/1";
                                    alert("hit:"+result);
                                    return kendo.stringify(result);
                                }
                                else if (operation !== "read" && options.models)
                                {
                                    return {models: kendo.stringify(options.models)};
                                }
                             }
                      }
                      ,
                      schema:
                      {
                        model:
                        {
                          id: "taskId",
                          fields:
                          {
                            taskId: { from: "TaskID",type: "number"},
                            title: { from: "Title",defaultValue: "No title", validation: { required: true }},
                            start: { type: "date", from: "Start" },
                            end: { type: "date", from: "End" },
                            description: { from: "Description" },
                            recurrenceId: { from: "RecurrenceID" },
                            recurrenceRule: { from: "RecurrenceRule" },
                            recurrenceException: {from:"RecurrenceException"},
                            ownerId: {from: "OwnerID", defaultValue: 1},
                            isAllDay: {type: "boolean",from:"IsAllDay"}
                          }
                        }
                      },
                       requestEnd: function(e)
                       {
                        var response = e.response;
                        alert("herenothing:"+response +"|"+JSON.stringify(response));
                         
                         
                      },
                       error: function(e) {
                        alert(e.status);
                       }
                }
               
            });
             
    var randdt=new Date();
    var teststr="KPIGetData/DimData.aspx?SearchInput=schedulerevent&rand="+ randdt;
    //var result ="&SearchValue="+CurrentUserID+"&StartTime="+SearchTimeStart+"&EndTime="+SearchTimeEnd;
    var result ="&SearchValue=475&StartTime=2014/1/1&EndTime=2015/1/1";
     
          jQuery.ajax({
                                        url: teststr,
                                        type: "POST",    
                                        data: result,
                                        beforeSend:function startloading(){
                                          
                                        },
                                         error: function (jqXHR, textStatus, errorThrown) {
                                            if (textStatus != "abort")
                                            {
                                              jQuery("#test").html(teststr+result+"&randd=123");
                                            }
                                        },                                                 
                                        success: function filltree(response) {
                                          alert("hereright:"+response +"|"+JSON.stringify(response));
                                        }
                     });                  

    to make it clear, I change all the paras into a fix value, and I am sure there is no JS error. And I run the code above.
    the result is, the pure jquery.ajax POST get the Json well, the result is: 
    hereright:[object Object]|[{"TaskID":"885"}]

    and the KendoUI result is:herenothing:undefined|undefined
    you could check the screencut in the files attached.

    And to rule out the reason maybe it's caused by my browser, I changed to chorme, and firefox, and the result is the same.

    So it seems that the KendoUI don't recognize the return string as Json.
    I swear I had met the same problem many years ago. But I did not wrote the solution, and forget it now.
    I am too stupid, I should write the solution in my note..

    yours,
    Ivan




     
  5. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 24 Nov 2014 Link to this post

    Hi Ivan,

    In the custom "ajax" method that you are using for testing, the "dataType" option is missing (currently configuration of the dataSource and "ajax" request differs only by this option)  - could you please try to add it and let us know of the result (most probably the content type is not correctly set to "json")?

    jQuery.ajax({
                url: teststr,
                type: "POST",   
                dataType: "json",
                data: result,
                beforeSend:function

    Regards,
    Vladimir Iliev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. ivan
    ivan avatar
    27 posts
    Member since:
    Aug 2013

    Posted 26 Nov 2014 in reply to Vladimir Iliev Link to this post

    hi, I tried, the result is not as you thought.
    var randdt=new Date();
    var teststr="KPIGetData/DimData.aspx?SearchInput=schedulerevent&rand="+ randdt;
    //var result ="&SearchValue="+CurrentUserID+"&StartTime="+SearchTimeStart+"&EndTime="+SearchTimeEnd;
    var result ="&SearchValue=475&StartTime=2014/1/1&EndTime=2015/1/1";
     
          jQuery.ajax({
                                        url: teststr,
                                        type: "POST",
                                        dataType: "json",    
                                        data: result,
                                        beforeSend:function startloading(){
                                          
                                        },
                                         error: function (jqXHR, textStatus, errorThrown) {
                                            if (textStatus != "abort")
                                            {
                                              jQuery("#test").html(teststr+result+"&randd=123");
                                            }
                                        },                                                 
                                        success: function filltree(response) {
                                          alert("hereright1:"+response +"|"+JSON.stringify(response));
                                        }
                     });

    As you see, I add the datatype with "json", and to make it sure about, I change the alert("hereright:  to  alert("hereright1:, so I could check. The result is:

    hereright1:[object Object]|[{"TaskID":"885"}]

    So the jQuery.ajax could get the json string, maybe the problem is on the server side.
    I give the whole server code in the attach file.

    The Url is "KPIGetData/DimData.aspx?SearchInput=schedulerevent&rand="+ randdt;
    So please just see the SearchInput=schedulerevent fragment, and ignore the others.

    else if (SearchInput == "schedulerevent")
                       {
                           Response.Clear();
                           Response.ContentType = "application/json";
                           Response.Charset = "UTF-8";
                           Response.Write(GetSchedulerEvent(SearchValue,StartTime, EndTime));
                           Response.End();
                       }

    Then could see the code in the GetSchedulerEvent function, as below:
      string sql = "select TableID,TaskName,TaskScript,AgentKey,CONVERT(varchar(100), StartTime, 20) as 'StartTime',CONVERT(varchar(100), FinishTime, 20) as 'FinishTime',EventRecurrenceID,EventRecurrenceRule,EventRecurrenceException,EventIsAllDay from OAOrderTaskFactBill where StartTime>='" + StartTime + "' and FinishTime<'" + EndTime + "' and AgentKey in (" + SearchValue + ") and isEventKey='1' order by TaskOrder";
                   
    dt = web.GetValueRunSQL(sql);
     
    string tes = dt.Rows[0]["TableID"].ToString();
    string test2 = "[{" + "\"" + "TaskID" + "\"" + ":" + "\"" + tes + "\"" + "}]";
    return test2;

    Please ignore the code in annotation, that is I tested the FIX variable which is with NO problem.
    And you could see if I create the json string from a datatable's record and the problem is there.

    And there is no compile error ofcause,  I could the the string return from the URL when I run it on the brower's address line.

    yours,
    Ivan
       




  7. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 27 Nov 2014 Link to this post

    Hello Ivan,

    I tried to reproduce the issue with sample data on our side but to no avail - the response is correctly accepted from the dataSource and loaded as item (as seen in this screencast). From this point I could only suggest to check the exact value returned from the dataBase as it makes the JSON response invalid on your side.

    Regards,
    Vladimir Iliev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  8. ivan
    ivan avatar
    27 posts
    Member since:
    Aug 2013

    Posted 27 Nov 2014 in reply to Vladimir Iliev Link to this post

    Iliev, thank you for your try, but please notice:

     returnstr = "\"" + "TaskID" + "\"" + ":" + "\"" + drdtv[t]["TableID"].ToString() + "\"";

    I only get the TableID from the database, which is a bigint and self-added key column, so it's a bigint number. 
    In fact, it's 885, the same as I tested the hard-coded way.
    I can't see it will makes the Json response invalid. 

    Need I bring a support Tickets about this?
    yours,
    Ivan
  9. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2194 posts

    Posted 01 Dec 2014 Link to this post

    Hi Ivan,

    Please note that without reproducing the issue on our side we could only guess what the real reason is - that why in order to investigate further we would need runable project where the issue is reproduced (please include sample dataBase as well).

    Regards,
    Vladimir Iliev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top