【问题标题】:Not Receiving the query result to ajax function from asmx page asp.net未从 asmx 页面 asp.net 接收到 ajax 函数的查询结果
【发布时间】:2023-03-11 04:20:02
【问题描述】:

我正在我的应用程序中使用 Ajax 和 jQuery。当我返回查询结果时,它没有向我显示该结果。代码如下:

asmx 页面代码

 [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 代码

 <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>  

它没有向我显示答案,因为当我删除该(字符串 id)参数时,它可以完美运行。当我使用它时,它并没有告诉我答案。我想完成我的项目,但这个错误并没有推动我。任何帮助请。

【问题讨论】:

    标签: c# jquery asp.net ajax


    【解决方案1】:

    您的第一个请求看起来不错。但是在您的第二个请求中,我没有看到您使用的是 jQuery DataTable data 选项。

    $(".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) {
    
                 }
             });
    

    【讨论】:

      【解决方案2】:

      我认为如果您更改数据以发送字符串,它会起作用

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

      另一种方法是做类似的事情

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-05
        • 2022-01-04
        • 1970-01-01
        • 2013-01-16
        相关资源
        最近更新 更多