我有一个WebMethod,它获取要在DataSet中填充DropDown的数据。目前,我正在使用硬编码对象填充下拉列表。但是我想用webmethod返回的数据替换这个硬编码的对象。
[System.Web.Services.WebMethod] public static string GetDropDownDataWM(string name) { //return "Hello " + name + Environment.NewLine + "The Current Time is: " // + DateTime.Now.ToString(); var msg = "arbaaz"; string[] name1 = new string[1]; string[] Value = new string[1]; name1[0] = "@Empcode"; Value[0] = HttpContext.Current.Session["LoginUser"].ToString().Trim(); DataSet ds = new DataSet(); dboperation dbo = new dboperation(); ds = dbo.executeProcedure("GetDropDownsForVendor", name1, Value, 1); return ds.GetXml(); }
客户端(更新1):
<script type = "text/javascript"> function GetDropDownData() { var myDropDownList = $('.myDropDownLisTId'); $.ajax({ type: "POST", url: "test.aspx/GetDropDownDataWM", data: '{name: "abc" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { $.each(jQuery.parseJSON(data.d), function () { myDropDownList.append($("<option></option>").val(this['FieldDescription']).html(this['FieldCode'])); }); }, failure: function (response) { alert(response.d); } }); } function OnSuccess(response) { console.log(response.d); alert(response.d); } </script>
function GetDropDownData() { $.ajax({ type: "POST", url: "test.aspx/GetDropDownDataWM", data: '{name: "abc" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function(data.d) { $.each(data.d, function (){ $(".myDropDownLisTId").append($("<option />").val(this.KeyName).text(this.ValueName)); }); }, failure: function () { alert("Failed!"); } }); }