【问题标题】:jquery datatables pluginis is not work in asp.net web servicesjquery datatables pluginis 在 asp.net Web 服务中不起作用
【发布时间】:2017-06-04 23:12:33
【问题描述】:

我正在做一个项目,我点击人类图像部分并显示症状。为此,我使用完美返回 json 数据的 Web 服务。这是我的 Web 服务代码:

public class SympsService : System.Web.Services.WebService
{
    [WebMethod]
    public void GetSymptoms(String organ_name)
    {
        List<symps> listSymptoms = new List<symps>();
        string CS=ConfigurationManager.ConnectionStrings["EhealtsCS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("sendSymptoms", con);
            cmd.CommandType = CommandType.StoredProcedure ;
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@organ";
            parameter.Value = organ_name;
            cmd.Parameters.Add(parameter); con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                symps symp = new symps();
                symp.Sympt = rdr["SymptomsName"].ToString();
                listSymptoms.Add(symp);

            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(listSymptoms));
        }

我使用 jquery datatables 插件来绑定表中的 json 数据。但是当我单击图像时,我的 jquery 代码没有在表上显示 json 数据。这是我的 Jquery 代码:

if (e.key === 'toes'){
          $.ajax({
                url: "SympsService.asmx/GetSymptoms",
                data:{ organ_name: toes},
                method: "post",
                dataType: "json",
                success: function (data){
                    var Variable = $('#symptomsTable').DataTable({
                        data: data,
                        columns: [ { 'data': 'Sympt' }]
                    });
                },
            error: function (error)
            {console.log(error);}
        });
    }

我也试过这个 jquery 在表中显示 json 数据,但这不起作用:

success: function (data) {
                    var symptomsTable = $('#symptomsTable tbody');
                    symptomsTable.empty();
                    $(data).each(function(index,symp) {
                        symptomsTable.append('<tr><td>' + symp.sympt + '</td></tr>');
                    });
                }

请,谁能告诉我我的代码有什么问题。任何形式的帮助将不胜感激。

【问题讨论】:

  • 在您的 success 元素之后,您能否通过添加 console.log(data) 并告诉我们您获得的信息来确保您获得了正确的数据?
  • @jonmrich 谢谢你..我只是检查一下。它显示 Uncaught ReferenceError: toes is not defined 在这一行:'data:{organ_name:toes}' 你能告诉我这是什么意思吗?
  • @jonmrich 谢谢你的建议......这是一个错误......当我使用 console.log(data) 时,我发现了。我需要使用“脚趾”而不是脚趾。
  • 很高兴它有帮助!

标签: jquery asp.net json web-services asp.net-ajax


【解决方案1】:

不确定这是否是最好的方法,但这是我通常从返回 json 的 web api 调用中水合我的数据表的方式。

我通常会创建一个函数来保存我的行。像

   function category(id, name, action) {
  var self = this;
  this.id = id;
  this.name = name;
  this.action = action;
}

function model() {
  var self = this;
  this.categories = [];
}

var mymodel = new model();

然后在我的 ajax 调用成功时,我将行推送到数组中。

success: function (data){
                  $.each(data, function( i, item ) {
                     mymodel.categories.push(new category(item.id, item.category, item.action));
  });
   var table = $('#mytable').DataTable({
    data: mymodel.categories,
    columns: [{
        data: 'id'
      }, {
        data: 'name'
      }, {
        data: 'action'
      }

    ],
    dom: '<"toolbar">frtip'

  });

                },

【讨论】:

  • @brayan 感谢您的 ud 建议。我也试试你的方法,它也有效。
【解决方案2】:

使用data:{ organ_name: "toes"} 而不是data:{ organ_name: toes}。 一定要仔细检查你的代码,否则你会像我一样傻。

【讨论】:

    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多