How to populate dropdownlist with JSON data as ajax response in jQuery

后端 未结 7 1235
梦谈多话
梦谈多话 2021-02-05 14:47

I am working on a j2ee application. In my application I have a drop-down list(or Select element). I want to populate this drop-down list with JSON data as a Ajax response.

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 15:27

    <%----%>
    -------------------------------------------------------------------------------- $(document).ready(function () { $.ajax({ type: "POST", url: "AjaxCallGrid.asmx/GetDropDown", dataType: "json", contentType: "application/json; charset=utf-8", success: function (result) { $('.DropDown').empty(); $('.DropDown').append(""); $.each(result.d, function (key, value) { $('.DropDown').append($("").val(value.iD).html(value.firstName)); }); } }); }); ------------------------------------------------------------------------- [WebMethod] public List GetDropDown() { DataTable dt = new DataTable(); List result = new List(); using (SqlConnection con = new SqlConnection(@"Data Source=DOS-PC\MARJI;Initial Catalog=examples;Integrated Security=True")) { using (SqlCommand cmd = new SqlCommand("select id,firstname from Students ", con)) { con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { result.Add(new Students { iD = Convert.ToInt32(dt.Rows[i]["id"].ToString()), firstName = dt.Rows[i]["firstname"].ToString() } ); } } return result; } }

提交回复
热议问题