gpt4 book ai didi

javascript - 在 Datalist 问题中填充数据

转载 作者:行者123 更新时间:2023-12-03 11:43:28 25 4
gpt4 key购买 nike

我正在使用以下 Javascript 函数来填充数据列表:

function GetDropDownData(f) {
$.ajax({
url: '/Rentals/Base/GetContactsForFacility?selectedFacility=' + f,
data: { facility: f },
dataType: 'json',
success: function (result) {
response($.map(result, function (item) {

$('#custServiceContactsSelection').append($("<option />").val(item.ContactName).text(item.ContactName));

}));
},

cache: false,
error: function (jqXHR, textStatus, errorThrown) {
if (errorThrown.indexOf("Your session has timed out") != -1) {
location.href = "/Rentals/Base/Timeout";
}
}
});
}

以下是我的 Controller 内的方法:

    public ActionResult GetContactsForFacility (string selectedFacility)
{
var facilityId = new Guid(selectedFacility);

if (Request.IsAjaxRequest())
{
var contacts = SessionService.AllCustomerServiceContactsForFacility(CrmService, facilityId);


return Json(contacts.Select(x => new { label = x.ContactName }), JsonRequestBehavior.AllowGet);
}
return Content(string.Empty);
}

当我尝试运行它时,它从 Controller 返回。但是,在那之后,我的 VS 中出现错误:JavaScript 运行时错误:“响应”未定义

我认为,函数 GetDropDownData() 中缺少某些内容,但无法弄清楚它到底是什么。

请你指导一下好吗?谢谢!

最佳答案

在您的 AJAX 请求中,您需要将其更改为:

success: function ( response ) {
$.map(response, function (item) {

$('#custServiceContactsSelection').append($("<option />").val(item.ContactName).text(item.ContactName));

});
},
// rest of code

关于javascript - 在 Datalist 问题中填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26164361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com