jQuery Generic Autocomplete Error 404

Asked

Viewed 71 times

0

I’m making a input with Autocomplete function only that when typing it returns in the console with error 404. Follow 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 anyone can help me where I’m going wrong I’d appreciate it.

  • autocomplete is jQuery or some other library? I’m not sure if it is possible to indicate how source some address, as in an ajax request, because if this is the case, you can first make an ajax request and then fill in the autocomplete with the result.

  • @Lucascosta , #Temosomesmonome! and yes to indicate url, is from jqury ui

  • give a look here https://forums.asp.net/p/1533299/3717070.aspx

  • I did not understand very well the link that sent @I'mBlueDaBaDee, then ListaAuto.ashx source is valid? In which return format will the values for autocomplete be filled in?

  • @Lucascosta the link is to Brayan, hahaha

2 answers

0


Basically we’ll 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 his Routes.cs let’s map this out:

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 his script looks something like this:

$(document).ready(function () {
    //Com nossa nova Rota
    $("#ListaNCM").autocomplete({ source: "Ncm/Autocomplete/{term}" });
});
  • First thank you kk, but... Keep giving error posted again the code as it turned out.

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 giving Notfound, there’s something wrong?

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • puts the error q appears on the console, let’s see where it’s sending

  • ready put in post

  • Amigo got/ kk, just put a {term} in the source tbm..... source: "Ncm/Autocomplete/{term}"

  • Thank you very much for your help

  • Denada kk, you want me to answer?

  • I think it would be interesting if someone needs it in the future, thank you very much :D

  • #Done, no need to thank ^^

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.