asp.net 来自网络的网络表单下拉列表 api

asp.net web form dropdownlist from web api

需要一些帮助,了解如何使用从 Web api (JSON) 获取的数据准确填充下拉列表。 尝试了多篇文章,但不知何故,它就是行不通。

我从 api 获得的数据: link: http://192.168.0.11/api/lookup

["Daniel","Mark","John"]

default.aspx 站点:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                var userrs = $('#userrs');

                $('#btnRefresh').click(function () {
                    $.ajax({
                        type: 'GET',
                        url: 'http://192.168.0.11/api/lookup',
                        dataType: 'json',
                        success: function (data) {
                            userrs.empty();
                            $.each(data, function (val) {
                                var userid = val
                                $("#DropDownList1").append('<option>' + userid + '</option >')

                            });
                        }
                    });
                });
            });
</script>
<body>

    <form id="form1" runat="server" class="auto-style1">
        <asp:Label ID="Label1" runat="server" Text="Select User" CssClass="table"></asp:Label>


        <br/>
        <asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="158px">
        </asp:DropDownList>
        <asp:Button ID="btnRefresh" runat="server" OnClick="btnRefresh_Click1" Text="Refresh" />
        <ul id= "userrs"></ul>
        <br />
        <asp:Button ID="btnSend" runat="server" OnClick="btnSend_Click1" Text="Send" />
        <br />
        <asp:TextBox ID="txtbox" runat="server" Height="104px" OnTextChanged="txtbox_TextChanged" Width="272px"></asp:TextBox>
        <br />
        </form>
</body>
</html>

目标是通过单击按钮 btnRefresh 将他从 api 获得的 3 个名称填充到 DropDownList1 中。

出于某种原因,它没有向下拉列表中添加任何内容。

提前致谢。

您需要一个索引。 (并添加分号。)

$.each(data, function (idx, val) {
    var userid = val;
    $("#DropDownList1").append('<option>' + userid + '</option >');    
});