skip to Main Content

I’m working with the Ajax and jQuery in my app. When I return a query result it’s not showing me that result. The Code is given below:

asmx page Code

 [WebMethod, ScriptMethod] 
 public void Getusertimelist(string id)
 {
     List<UserhoursList> employeelist = new List<UserhoursList>();
     using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString)) 
    { 
        con.Open();
        SqlCommand command12 = new SqlCommand(" SELECT CONVERT(TIME, DATEADD(s, SUM(( DATEPART(hh, Total_time) * 3600 ) + ( DATEPART(mi, Total_time) * 60 ) + DATEPART(ss, Total_time)), 0)) From Todaywork Where Id='"+id+"'", con);

        string getvalue = command12.ExecuteScalar().ToString();

        UserhoursList employee = new UserhoursList();
        employee.Total_Time = getvalue;
        employeelist.Add(employee);
        con.Close();
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Write(js.Serialize(employeelist));
    //return id;
}

Ajax Code

 <script type="text/javascript">  

     $(document).on("click", ".get_time", function () {

         var timeid = $(this).data('id');

         $.ajax({
             url: 'UserTotaltime.asmx/Getusertimelist',
             method: 'post',
             data: { id: timeid },
             success: function (data) {
                 //alert(data);
             },
             error: function (err) {

             }
         });
     });

     $(document).ready(function () {

         $(".get_time").click(function () {

             $.ajax({
                 url: "UserTotaltime.asmx/Getusertimelist",
                 dataType: "json",
                 method: 'post',
                 success: function (data) {
                     alert(data[0].Total_Time);
                     //  var prodetail = document.getElementById('textbox').HTML = 2;
                     document.getElementById("UserTime").innerHTML = data[0].Total_Time;
                     prodetail.html(data[0].Total_Time);

                 },
                 error: function (err) {

                 }
             });

         });
     });
</script>  

It’s not showing me the answer because when I remove that (string id) parameter then it works perfectly. When I use it’s not showing me the answer. I want to complete my project but this error not pushing me. Any help plz.

2

Answers


  1. Your first request looks good. But in your second request, I don’t see that you’re using the jQuery DataTable data option.

    $(".get_time").click(function () {
    
             $.ajax({
                 url: "UserTotaltime.asmx/Getusertimelist",
                 dataType: "json",
                 method: 'post',
                 // Where is data: { id: timeid }?
                 success: function (data) {
                     alert(data[0].Total_Time);
                     //  var prodetail = document.getElementById('textbox').HTML = 2;
                     document.getElementById("UserTime").innerHTML = data[0].Total_Time;
                     prodetail.html(data[0].Total_Time);
    
                 },
                 error: function (err) {
    
                 }
             });
    
    Login or Signup to reply.
  2. I think it will work if you change data to send a string

             $.ajax({
                 url: 'UserTotaltime.asmx/Getusertimelist',
                 method: 'post',
                 data: '{ "id": ' + timeid + '}',
                 success: function (data) {
                     //alert(data);
                 },
                 error: function (err) {
    
                 }
             });
    

    an alternative is do something like

     var dat = {}
     dat.id = timeid
    
              $.ajax({
                 url: 'UserTotaltime.asmx/Getusertimelist',
                 method: 'post',
                 data: JSON.stringify(dat),
                 success: function (data) {
                     //alert(data);
                 },
                 error: function (err) {
    
                 }
             });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search