Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery未定义每个项_Jquery_Asp.net_Webforms - Fatal编程技术网

jquery未定义每个项

jquery未定义每个项,jquery,asp.net,webforms,Jquery,Asp.net,Webforms,My WebApi正在返回以下JSON对象: [ { "Department_Id": 5, "Department_Description": "Test API 1" }, { "Department_Id": 6, "Department_Description": "Test API 2" }, { "Department_Id": 7, "Department_Description": "Test API 3" } ] 在使用asp.

My WebApi正在返回以下JSON对象:

[
{
    "Department_Id": 5,
    "Department_Description": "Test API 1"
},
{
    "Department_Id": 6,
    "Department_Description": "Test API 2"
},
{
    "Department_Id": 7,
    "Department_Description": "Test API 3"
}
]
在使用asp.net的web表单中,我有以下代码:

<script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            type: "POST",
            url: "api/TicketDepartments/GetTicketDepartmentsComboBoxAsync",
            data: "{}",
            contentType: 'application/json; charset=utf-8',
            dataType: "json",
            async: false,
            success: function (data) {
                var select = $("#ddlDepartment");

                if (data) {
                    $(data).each(function(index, item) {
                        select.append($(" 
<option>").val(item.Value).text(item.Text));
                    });
                }
            },
            error: function (data) {
                alert("An error has occurred during processing your 
 request.");
            }
        });
    });
   </script>
使用部门描述和部门id,以便以后可以在程序中使用


我不确定这个错误是什么意思,也不确定它来自哪里。欢迎任何帮助

我认为您的代码应该是:

select.append($("<option>").val(item.Department_Id).text(item.Department_Description));
                    });
select.append($(“”).val(item.Department\u Id).text(item.Department\u Description));
});
each()方法用于在包含DOM元素的jQuery集合中循环。如果要在普通数组或对象中循环,应使用
$.each()
函数

$.each(data, function(index, item) {
    ...
}

此外,对象中的属性是
Department\u ID
Department\u Description
,而不是
Value
Text

错误的原因是
,而不是属性名称。
$.each(data, function(index, item) {
    ...
}