query ajax json 通过后台代码(查询数据库)绑定html控件select 下拉框实例

时间:2022-12-09 16:23:37

前台代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>json测试</title>

‍<script src="../../js/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">

        $(document).ready(function() {

               $.ajax({
                url: "test.asmx/GetSupplier",    //后台webservice里的方法名称
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                data: '{}',
                traditional: true,
                success: function(data) {
                    for (var i in data) {
                        var jsonObj = $.parseJSON(data[i]);
                        var optionstring = "";
                        for (var item in jsonObj) {
                            jsonObj = eval(jsonObj.Table)
                            for (var i = 0; i < jsonObj.length; i++) {
                                optionstring += "<option value=\"" + jsonObj[i].id + "\" >" + jsonObj[i].name + "</option>"; ;
                            }
                            //                            alert(optionstring);

                        }
                        $("#ddl1").html(optionstring);
                    }
                },
                error: function(msg) {
                    alert("出错了!");
                }
            });
        });

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>

    <select id="ddl1" name="supplier"></select>  

    </div>
    </form>
</body>
</html>

 

后台websevice里的方法:

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    ///绑定供应商 
    public string GetSupplier()
    {
        string ReturnValue = string.Empty;
        string sql = "select * from tb_supplier";   //sql语句
        DataTable dt = Common.GetDataSet(sql);  //数据库帮助类 返回databale类型
        ReturnValue = DataTableJson(dt);
        return ReturnValue;
    }

    #region dataTable转换成Json格式
    /// <summary>    
    /// dataTable转换成Json格式    
    /// </summary>    
    /// <param name="dt"></param>    
    /// <returns></returns>    
    public string DataTableJson(DataTable dt)
    {
        StringBuilder jsonBuilder = new StringBuilder();
        jsonBuilder.Append("{\"");
        jsonBuilder.Append(dt.TableName.ToString());
        jsonBuilder.Append("\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            jsonBuilder.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                jsonBuilder.Append("\"");
                jsonBuilder.Append(dt.Columns[j].ColumnName);
                jsonBuilder.Append("\":\"");
                jsonBuilder.Append(dt.Rows[i][j].ToString());
                jsonBuilder.Append("\",");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("},");
        }
        jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
        jsonBuilder.Append("]");
        jsonBuilder.Append("}");
        return jsonBuilder.ToString();
    }
    #endregion