locked
Unable to cast object of type : Jquery Ajax method to call a webservice RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    I am calling a webservice through jquery ajax method. Below is my service method.

    [WebMethod]
        public DataTable GetTrainingDates(int user_id)
        {
            DBHelper oDBHelper = new DBHelper();
            Hashtable param = new Hashtable();
            param.Add("@case",6);
            param.Add("@book_by ", user_id);
            DataTable dt = new DataTable();
            dt = oDBHelper.GetDatatabel("[dbo].[usp_training_update]", param);
            dt.TableName = "MyTable";
            return dt;
        }

    and below is my jquery code to call this method. I am trying to get parameter from a Literal names ltuser. see the below 

    <script type="text/javascript">
            debugger;
            var uid = $('#<%=ltuser.ClientID%>').html();
            var id = parseInt(uid);
            $.ajax({
                type: "POST",
                url: "ServiceGetTrainingDate.asmx/GetTrainingDates",
                data: "{'user_id': " + id + " }",
                contentType: "application/json",
                datatype: "json",
                success: function (data) {
                    $('div[id*=calender]').fullCalendar({
                        header: {
                            left: 'prev,next today',
                            center: 'title',
                            right: 'month,agendaWeek,agendaDay'
                        },
                        editable: true,
                        events: data.d
                    });
                    $("div[id=loading]").hide();
                    $("div[id=celender]").show();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                }
            });
    
            debugger;
            $('div[id*=calender]').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,agendaWeek,agendaDay'
                },
                editable: true,
                events: $.map(data.d, function (item, i) {
                    var event = new Object();
                    event.bigid = item.EventID;
                    event.vcexpirydate = new Date(item.EndDate);
                    event.title = item.EventName;
                    return event;
                })
            });
        </script>

    I tried to convert this ltuser value to int as my method requires an int, but still it shows Unable to cast object of type.

    Please help.

    Friday, February 22, 2019 5:30 AM

Answers

  • User-2054057000 posted

    You are using a literal control which is bad because literal control is not rendered as any HTML control in the browser.

    I did some testing for the literal control in my web form. I added the following code in my .aspx page:

    <div id="LtDiv">
        <asp:Literal ID="ltuser" runat="server">12</asp:Literal>
    </div>

    In my browser it was rendered as:

    <div id="LtDiv">
       12
    </div>

    You can see the literal (id='ltuser') is not rendered at all, only its value is shown.

    I suggest you to use span instead of literal:

    <span id="ltuser">12</span>

    and you can get it's value like this:

    $("#ltuser").text();

    And then you can make the AJAX request to your web service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2019 11:06 AM

All replies

  • User-2054057000 posted

    You are using a literal control which is bad because literal control is not rendered as any HTML control in the browser.

    I did some testing for the literal control in my web form. I added the following code in my .aspx page:

    <div id="LtDiv">
        <asp:Literal ID="ltuser" runat="server">12</asp:Literal>
    </div>

    In my browser it was rendered as:

    <div id="LtDiv">
       12
    </div>

    You can see the literal (id='ltuser') is not rendered at all, only its value is shown.

    I suggest you to use span instead of literal:

    <span id="ltuser">12</span>

    and you can get it's value like this:

    $("#ltuser").text();

    And then you can make the AJAX request to your web service.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 22, 2019 11:06 AM
  • User-474980206 posted

    I have not used webforms and thus [WebMethod] in years, but following current json binding rules, you are pass an object not an int. there are 2 solutions:

    1) change ajax data:

    // convert to string for data 
    data: id + '',
    // use standard JSON utility
    data: JSON.stringify(id),

    2) change the web method to receive an object:

    public class UserIdRq
    {
        public int user_id {get; set;}
    }
    [WebMethod]
    public DataTable GetTrainingDates(UserIdRq user_id)
    {
    ....
    } 

    Friday, February 22, 2019 8:16 PM