JQuery从通用处理程序中检索多个值

时间:2016-12-22 05:17:27

标签: jquery generic-handler

在Google上搜索并阅读文档后,我很困惑这是如何工作的。使用值调用泛型处理程序并在成功时返回多个值。你如何循环数据库中的记录?您是否通过JQuery Every Function循环遍历通用处理程序或成功的记录?下面是我当前的代码,当存在多个值时,该代码不起作用。删除$ .each(vardata,function()并且它有效;但是,只显示一条记录。

在文本框中输入Erie并单击按钮

后,这就是数据的外观
Business Profile ID: 8 
Business Name: The Boston Store
Phone Number: 814-455-1478
E-Mail: BostonStore@gmail.com

Business Profile ID: 9
Business Name: Sam The Man Pizza
Phone Number: 814-868-3809
E-Mail: Ed@samtheman.com

JQuery脚本

    $(document).ready(function () {
        $('#button').click(function () {
            $.ajax({
                contentType: "text/html; charset=utf-8",
                data: "ID=" + $('#businessSelect').val(),
                url: "getTest.ashx",
                dataType: "text",
                success: function (data) {
                    var vardata = JSON.parse(data);
                    $.each(vardata, function (index, value) {
                        $("#BusProfileID").html(value.BusProfileID);
                        $("#BusinessName").html(value.BusinessName);
                        $("#BusinessPhone").html(value.BusinessPhone);
                        $("#BusinessEmail").html(value.BusinessEmail);
                    });
                }
            })
        });
    });

ashx处理程序页面

     public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        string ID = context.Request.QueryString["ID"];
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT BusProfileID, BusinessName, BusinessPhone, BusinessEmail FROM [BusProfile] WHERE BusinessCity = @BusinessCity", conn);
        comm.Parameters.Add("@BusinessCity", System.Data.SqlDbType.VarChar);
        comm.Parameters["@BusinessCity"].Value = ID;
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            List<BusinessData> objList = new List<BusinessData>();
            BusinessData objData;
            while (reader.Read())
            {
                objData = new BusinessData();
                objData.BusProfileID = reader["BusProfileID"].ToString();
                objData.BusinessName = reader["BusinessName"].ToString();
                objData.BusinessPhone = reader["BusinessPhone"].ToString();
                objData.BusinessEmail = reader["BusinessEmail"].ToString();
                objList.Add(objData);
                context.Response.Write(JsonConvert.SerializeObject(objList));
            }
            reader.Close();
        }

        finally
        {
            conn.Close();
        }
    }

    public class BusinessData
    {
        public string BusProfileID { get; set; }
        public string BusinessName { get; set; }
        public string BusinessPhone { get; set; }
        public string BusinessEmail { get; set; }
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

ASPX页面

            <div class="row">
                <div class="columns medium-12">
                    <div class="row">
                       <div class="medium-6 columns medium-centered">
                            <label style="font-size:1em">Select City:</label>
                            <input type="text" id="businessSelect" style="height:2em;" /> <input type="button" id="button" value="Click me" />
                      </div>
                    </div>
                    <div class="row">
                       <div class="medium-6 columns medium-centered">
                            <label style="font-size:1em">Business Profile ID:</label>
                            <label id="BusProfileID" style="font-size:1em;"></label>
                      </div>
                    </div>
                    <div class="row">
                       <div class="medium-6 columns medium-centered">
                            <label style="font-size:1em">Business Name:</label>
                            <label id="BusinessName" style="font-size:1em;"></label>
                      </div>
                    </div>
                    <div class="row">
                        <div class="medium-6 columns medium-centered">
                            <label style="font-size:1em">Phone Number:</label>
                            <label id="BusinessPhone" style="font-size:1em;"></label>
                        </div>
                    </div>
                    <div class="row">
                        <div class="medium-6 columns medium-centered">
                            <label id="BusinessEmail" style="font-size:1em">E-Mail:</label>
                        </div>
                    </div>
                </div>

1 个答案:

答案 0 :(得分:0)

如果您希望通过结果loop,则必须是object。现在你stringify你的结果。所以你不能迭代它。

使用JSON.parse将结果转换为JSON对象,然后在该对象上使用$.each

var vardata = JSON.parse(data);
$.each(vardata, function (index, value) {
    // TODO
});

所以你不需要拆分并进入array。您可以在value语句中引用each参数。

修改

要使用JSON中的值,您可以使用服务器结果中设置的属性。

像,

$.each(vardata, function (index, value) {
    $("#BusProfileID").html(value.BusProfileID);
    $("#BusinessName").html(value.BusinessName);
    $("#BusinessPhone").html(value.BusinessPhone);
    $("#BusinessEmail").html(value.BusinessEmail);
});

使用class保留您从SQL Query

检索到的数据
public class BusinessData
{
    public long BusProfileID { get; set; }
    public string BusProfileID { get; set; }
    public string BusProfileID { get; set; }
    public string  BusProfileID { get; set; }
}

创建一个List<BusinessData>对象,并将阅读器中的每个项目添加到此列表

reader = comm.ExecuteReader();
List<BusinessData> objList = new List<BusinessData>();
BusinessData objData;
while (reader.Read())
{
    objData = new BusinessData();
    objData.BusProfileID = reader["BusProfileID"].ToString();
    objData.BusinessName = reader["BusinessName"].ToString();
    objData.BusinessPhone = reader["BusinessPhone"].ToString();
    objData.BusinessEmail = reader["BusinessEmail"].ToString();
    objList.Add(objData);
    context.Response.Write(JsonConvert.SerializeObject(objList));
}
reader.Close();

JsonConvert包含在Newtonsoft.Json nuget包中。