简体   繁体   English

如何绑定 Jquery Ajax Json 响应下拉列表?

[英]How can I bind Jquery Ajax Json Reponses to a dropdown?

I'm trying to populate Json response from an Ajax call to a drop down and bind Name and UserID in a dropdown.我正在尝试将 Ajax 调用中的 Json 响应填充到下拉列表中,并在下拉列表中绑定名称和用户 ID。 Dropdown values all shows undefined.下拉值都显示未定义。 What I'm doing wrong here?我在这里做错了什么? Can you please help?你能帮忙吗?

Dropdown DIV -下拉 DIV -

<div class="row form-group spacer">
                            <div class="col-md-12">
                                <div class="col-md-12">
                                    @Html.Label("Recipients")
                                    <select id="commentrecipients" class="dirtyignore" name="commentrecipients"></select>
                                </div>
                            </div>
                        </div>

Ajax Call - Ajax 调用 -

$.ajax({
        type: "GET",
        url: "/Submission/SecurityGroupsUsersAccessRight",
        data: {
            id: 214
        },
        success: function (data) {

            var s = '<option value="-1">Please Select a Recipient</option>';
            for (var i = 0; i < data.length; i++) {
                s += '<option value="' + data[i].UserID + '">' + data[i].Name + '</option>';
            }
            $("#commentrecipients").html(s);
        }
    }); 

Json Response - Json 响应 -

data = "[{"SecurityGroupID":31,"SecurityGroupName":"Permission Testers","UserID":30,"Name":"Dawn Test'Neil"},{"SecurityGroupID":31,"SecurityGroupName":"Permission Testers","UserID":213,"Name":"Dawn 2 Bates"}]"

在此处输入图像描述

You need to parse the JSON data to get the object and then loop it.需要解析JSON数据得到object然后循环。

ajax({
        type: "GET",
        url: "/Submission/SecurityGroupsUsersAccessRight",
        data: {
            id: 214
        },
        success: function (data) {
            let response = JSON.parse(data);
            var s = '<option value="-1">Please Select a Recipient</option>';
            for (var i = 0; i < response.length; i++) {
                s += '<option value="' + response[i].UserID + '">' + response[i].Name + '</option>';
            }
            $("#commentrecipients").html(s);
        }
    }); 

Try adding dataType: "json" and remove the "data:"... something like this:尝试添加 dataType: "json" 并删除 "data:" ... 像这样:

$.ajax({
    type: "GET",
    url: "/Submission/SecurityGroupsUsersAccessRight",
    dataType: "JSON",
    success: function (data) {

        var s = '<option value="-1">Please Select a Recipient</option>';
        for (var i = 0; i < data.length; i++) {
            s += '<option value="' + data[i].UserID + '">' + data[i].Name + '</option>';
        }
        $("#commentrecipients").html(s);
    }
}); 

Please try using the camel case property name:-请尝试使用驼峰式属性名称:-

s += '<option value="' + data[i].userID + '">' + data[i].name + '</option>';
$.ajax({
    type: "GET",
    url: "/Submission/SecurityGroupsUsersAccessRight",
    data: {
        id: 214
    },
    dataType: "json",
    success: function (data) {
        $.each(data.d, function (i, val) { 
            var s = '<option value="-1">Please Select a Recipient</option>';
            s += '<option value="' + val.UserID + '">' + val.Name + '</option>';
        }
        $("#commentrecipients").html(s);
    }
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM