【问题标题】:Ajax call not triggeringAjax 调用未触发
【发布时间】:2019-10-18 04:14:32
【问题描述】:

我想获取员工详细信息以进行自动完成 下面是我的 JS 代码,使用断点我可以确认它进入 else 条件, 但没有命中 ajax 调用。

$("#EmpName").autocomplete({
            source: function (request, response) {
                if (request.term.length <= 1) {
                }
                else {
                    $.ajax({
                        url: "/EmployeeController/GetEmployeeList",
                        type: "POST",
                        dataType: "json",
                        data: request,
                        success: function (data) {
                            var rows = new Array(data.length);
                            var count = -1;
                            if (data.length > 0) {
                                response($.map(data, function (Label, Value) {
                                    count++;
                                    return {
                                        label: data[count].Label,
                                        value: data[count].Label
                                    };
                                }))
                            }
                            else {
                                response($.map(request, function (Label, Value) {
                                    return {
                                        label: "No result found",
                                        value: ""
                                    };
                                }))
                            }
                        }
                    })
                }
            }
        });

以下是我在控制器中的代码

[HttpPost]
[ActionName("GetEmployeeList")]
public IActionResult GetEmployeeList(string term)
{
     //Create an object for  GetEmployeeList method
     return Json(ServiceLocator.Resolve<EmployeeService>().GetEmployeeList(term));
}

提前致谢。

【问题讨论】:

  • 你在你的 ajax 函数中加入了 try catch wrap 吗?检查确切的错误来自?
  • 检查开发人员工具中的网络选项卡,请求可能已发出但出错了。您应该使用错误或.catch() 回调来检查
  • 尝试将您的dataType 替换为application/json
  • 有没有机会分享一些关于您的路由设置的信息?如果不了解您的设置或引发的潜在错误,很难给出一个好的答案。网络选项卡是否为您提供更多信息,您的控制台是否有错误?

标签: javascript .net ajax


【解决方案1】:

您正在调用 EmployeeController(我冒昧地猜测您正在使用带有标准路由的 MVC 之类的框架)这不应该是 url: "/Employee/GetEmployeeList" 而不是 url: "/EmployeeController/GetEmployeeList" 吗?

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多