【问题标题】:Get data by Id using AJAX | JSON ASP.NET使用 AJAX 通过 Id 获取数据 | JSON ASP.NET
【发布时间】:2020-10-26 14:05:09
【问题描述】:

我有这个网络服务方法

List<object[]> List1 = new List<object[]>();
        [WebMethod(EnableSession = true)]
        [System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        public List<object[]> GetData(int ID)
        {
            var team = db.TEST.WHERE(a => a.id == ID).ToList();
            List1.Add(new object[]
                            {
                                team
                            });
            return teamList;
        }

这是我使用 Javascript & jQuer & Json 的函数

function BindList(id) {
            $.ajax({
                url: "/WebService1.asmx/GetData",
                type: "GET",
                dataType: "json",
                contentType: "application/Json; Charset= Utf-8",
                success: function (data) {
                    var list = "";
                    $.each(data.d[0][0], function (index, item) {
                        list += "<li class='text-secondary p-1'><a class='btn-link text-secondary'><b>" + item.Id+ "</b>" + ", " + "<span class='font-italic'><small>" + item.Name + "</small></span><small><a href='#' Onclick='Delete(" + item.Name + ")'> <i class='float-right far fa-trash-alt'></i></a></small></a>" + "</li>";
                    });
                    $("#list").html(list);// and this to print data to this list id 
                },
                error: function (response) {
                    alert(response);
                }
            });
        }

这是我获取数据的按钮

<input id="Button1" type="button" onclick="BindMembersList(1)" value="button" />

我的问题是我需要将 id 放在哪里,只提供 id 等于 1 的数据 谢谢

【问题讨论】:

    标签: javascript jquery asp.net json ajax


    【解决方案1】:

    由于它是您正在调用的 url,因此将其传递给您的 AJAX 调用的 url,如下所示:

    function BindList(id) {
            $.ajax({
                url: "/WebService1.asmx/GetData/" + id,
                type: "GET",
                dataType: "json",
                contentType: "application/Json; Charset= Utf-8",
                success: function (data) {
                    var list = "";
                    $.each(data.d[0][0], function (index, item) {
                        list += "<li class='text-secondary p-1'><a class='btn-link text-secondary'><b>" + item.Id+ "</b>" + ", " + "<span class='font-italic'><small>" + item.Name + "</small></span><small><a href='#' Onclick='Delete(" + item.Name + ")'> <i class='float-right far fa-trash-alt'></i></a></small></a>" + "</li>";
                    });
                    $("#list").html(list);// and this to print data to this list id 
                },
                error: function (response) {
                    alert(response);
                }
            });
        }
    

    如果它不起作用或者您需要向操作传递更多参数,您可以指定它们并使用“&”作为分隔符,如下所示:

    url: "/WebService1.asmx/GetData?id=" + id + "&parameter2=" + param,
    

    无论如何,你的 c# 方法是错误的,因为你返回的是 teamList,它没有在 action 内部实现

    【讨论】:

      【解决方案2】:

      试试这个网址:

      url: "/WebService1.asmx/GetData?id="+id
      

      【讨论】:

        猜你喜欢
        • 2012-04-10
        • 2012-04-28
        • 2019-12-09
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多