C#MVC5 jQuery自动完成,无法返回JSON数据

时间:2019-06-11 10:02:52

标签: c# mysql asp.net-mvc-5 jquery-ui-autocomplete

我是编程的初学者,我研究了一些实现jquery autocomplete的地方。我设法回发到JSON GROUP method。但是,成功之后,我将无法获得JSON results或编码错误。请帮助

代码

$(function () {
    $("#txtGRP_CODE").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/AutoComplete/GRP_CODE',
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: "{ 'prefix': '" + request.term + "'}",
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.Name, value: item.Name };
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("#txtGRP_CODE").val(i.item.value);
        },
        minLength: 1
    });
});

服务器端

[HttpPost]
public JsonResult GRP_CODE(string prefix)
    {
        List<AutoCompleteController> listGroup = new List<AutoCompleteController>();

        string sSQL = " SELECT * FROM icgrp WHERE GRP_CODE like '" + prefix + "%'";
        DataTable dt = conn2.GetData(sSQL);
        using (MySqlDataReader dr = conn2.ExecuteReader(sSQL))
        {
            //foreach (DataRow dr in ds.Tables[0].Rows)
            while (dr.Read())
            {
                listGroup.Add
                (new search
                    {
                        Name = dr["GRP_CODE"].ToString(),
                        Sr_no = dr["GRP_PART"].ToString()
                    }
                );
            }
        }

       //**I manage to populate listGroup but when passing it to the client side I can't get the Json data. 

        return Json(listGroup, JsonRequestBehavior.AllowGet);
    }

服务器端

https://ibb.co/sVbKSYG

客户端

https://ibb.co/09CFXdW

网络客户端响应

https://ibb.co/BB61dRd

回复

Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

 Requested URL: /AutoComplete/GRP_CODE

 Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0

0 个答案:

没有答案