jQuery AutoComplete with GenericHandler Error 404

0

I'm doing an input with an AutoComplete function, but typing it returns on the console with a 404 error. Here's the code:

$(document).ready(function () {
    $("#ListaNCM").autocomplete({ source: "ListaAuto.ashx" });
});

And the Generic Handler:

public class ListaAuto : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {          
        string input = context.Request.QueryString["term"];
        ProjetoEntities db = new ProjetoEntities();
        var Lista = db.Item.Where(x => x.DESCRICAO.Contains(input)).ToList();
        context.Response.Write(new JavaScriptSerializer().Serialize(Lista));
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

If someone can help me where I'm wrong, I appreciate it.

    
asked by anonymous 03.03.2017 / 14:34

2 answers

0

Basically let's create a route for your IHttpHandler :

public class MeuRouteHandler : IRouteHandler //Carinha que vai "apontar" pra nossa rota
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new ListaAuto();
        }

        private class ListaAuto : IHttpHandler
        {
            public bool IsReusable { get { return false; } }

            public void ProcessRequest(HttpContext context)
            {
                 string input = context.Request.QueryString["term"];

                 ProjetoEntities db = new ProjetoEntities();

                 var Lista = db.Item.Where(x => x.DESCRICAO.Contains(input))
                                    .Select(x => new { //autocomplete trabalha com label e value 
                                        label = x.DESCRICAO,//Aparece pro usuário
                                        value = x.ID//Valor 
                                    })
                                    .ToList();

                 context.Response.Write(new JavaScriptSerializer().Serialize(Lista));
            }
        }
    }

In your Routes.cs let's map this:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");

            //Bixisse pra deixar a rota bonitinha (Eu aprovo u.u)
            routes.Add(new Route("Ncm/Autocomplete/{term}", new MeuRouteHandler()));
            //Suas outras configurações ->...
        }
    }

Now your script looks something like this:

$(document).ready(function () {
    //Com nossa nova Rota
    $("#ListaNCM").autocomplete({ source: "Ncm/Autocomplete/{term}" });
});
    
03.03.2017 / 15:51
0
 public class AutoCompletaNCM : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new ListaAuto();
    }

    private class ListaAuto : IHttpHandler
    {
        public bool IsReusable { get { return false; } }

        public void ProcessRequest(HttpContext context)
        {
            string input = context.Request.QueryString["term"];

            ProjetoEntities db = new ProjetoEntities();

            var Lista = db.item.Where(x => x.DESCRICAO.Contains(input))
                               .Select(x => new
                               { //autocomplete trabalha com label e value 
                                   label = x.DESCRICAO,//Aparece pro usuário
                                   value = x.ID//Valor 
                               })
                               .ToList();

            context.Response.Write(new JavaScriptSerializer().Serialize(Lista));
        }
    }

}

RouteConfig:

 routes.Add(new Route("Ncm/Autocomplete/{term}", new AutoCompletaNCM()));

Jquery:

 $(document).ready(function () {
        //Com nossa nova Rota
        $("#ListaNCM").autocomplete({ source: "Ncm/Autocomplete" });
    });

So ... Keep on giving NotFound, is there something wrong?

    
03.03.2017 / 16:55