0

I am a beginner in programming, I have researched a few places to implement the jquery autocomplete. I managed to call postback to JSON GROUP method. But after success, I can't manage to get the JSON results or if I code it wrongly. Please help

Code

$(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
    });
});

Server Side

[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);
    }

Server Side

https://ibb.co/sVbKSYG

Client side

https://ibb.co/09CFXdW

Network Client Response

https://ibb.co/BB61dRd

Response

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
5
  • data: "{ 'prefix': '" + request.term + "'}" should be just data: {prefix: request.term }. Possible is duplicate stackoverflow.com/questions/12370614/… Commented Jun 11, 2019 at 10:11
  • Also could you provide network client response from your request (network tab in chrome devtools)? Commented Jun 11, 2019 at 15:51
  • @OlegBondarenko Hi I just attached the Network Client Response and also the response I get. Thank you Commented Jun 12, 2019 at 1:46
  • Where from you received HTTP 404 ? Because client side received 500 error. Also could you note your controller api header class ? Commented Jun 12, 2019 at 5:27
  • It seems issue with serialization of your List<AutoCompleteController> listGroup object. Is it really MVC controller class? Commented Jun 12, 2019 at 18:41

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.