How to fill a DropDown using Jquery Ajax Call?

 var theDropDown = document.getElementById("myDropDownLisTId");
                theDropDown.length = 0;
                $.each(items, function (key, value) {

                    $("#myDropDownLisTId").append($("<option></option>").val(value.PKId).html(value.SubDesc));

here "SubDesc",PKId describes the value getting out of Database., u need to separate your value from dataset.


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!");
        }
    });
}

From the WebMethod, don't send DataSet directly, send XML...

[System.Web.Services.WebMethod]
public static string GetDropDownDataWM(string name)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("Table0");
    ds.Tables[0].Columns.Add("OptionValue");
    ds.Tables[0].Columns.Add("OptionText");
    ds.Tables[0].Rows.Add("0", "test 0");
    ds.Tables[0].Rows.Add("1", "test 1");
    ds.Tables[0].Rows.Add("2", "test 2");
    ds.Tables[0].Rows.Add("3", "test 3");
    ds.Tables[0].Rows.Add("4", "test 4");

    return ds.GetXml();
}

Before Ajax call...

var myDropDownList = $('.myDropDownLisTId');

Try like below...(inside Ajax call)

success: function (response) {
    debugger;

    $(response.d).find('Table0').each(function () {
           var OptionValue = $(this).find('OptionValue').text();
           var OptionText = $(this).find('OptionText').text();
           var option = $("<option>" + OptionText + "</option>");
           option.attr("value", OptionValue);

           myDropDownList.append(option);
     });
},

Note:

  1. OptionValue and OptionText are the Columns of DataSet Table.

  2. $(response.d).find('Table0').each(function (){}) - Here Table0 is the name of Table inside DataSet.