getJSON with parameter

Asked

Viewed 453 times

-2

I am performing a query with getJSON calling the MVC controller and can return a Jsonresult. When I pass a letter to search for the first time the controller is triggered and returns the value filtered by the letter infomada, but if I delete this letter from the field and type itthere again, the controller is no longer triggered and getJSON somehow stored the first query and displays the result returned from the first query again.

Follow the passage.

$.getJSON("/Funcionalidade/FindWithByTelaTop", { Id_Sistema: id_sistema, Id_Grupo: $("#GrupoIdUpd").val(), Filtro: filtro, Top: 7 }, function (data) {
                    $.each(data.Records, function (i, item) {

                        TblTelaAddRow(item);
                        hasRow = true;
                    });

                    if (hasRow == false) {
                        if ($("#spanMessage").html().length == 0) {
                            $("#spanMessage").html("A consulta não retornou resultados");
                        }
                    } else {
                        if ($("#spanMessage").html().length > 0) {
                            $("#spanMessage").html("");
                        }
                    }
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(textStatus + " - " + errorThrown);
                });

Controller

public JsonResult FindWithByTelaTop(int Id_Sistema, int Id_Grupo, string Filtro = "", int Top = 10)
        {
            try
            {
                var dados = _funcionalidadeModels.find(Id_Sistema, Id_Grupo, Filtro)               
                    .Select(x => new
                    {
                        x.Id,
                        x.Tela                        
                    }).AsParallel();

                if (dados != null)
                {
                    return Json(new
                    {
                        Result = "OK",
                        Records = dados.OrderBy(o => o.Tela).Take(Top),
                        TotalRecordCount = dados.Count()
                    }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    string msg = "Erro ao tentar obter lista. Objeto nulo.";
                    return Json(new { Result = "ERROR", Records = msg }, JsonRequestBehavior.AllowGet);
                }
            }
            catch (Exception ex)
            {
                string msg = "Erro ao tentar consultar lista de clientes. Info: " + ex.Message;
                if (ex.InnerException != null)
                {
                    msg += ". Exceção interna: " + ex.InnerException.Message;
                }
                return Json(new { Result = "ERROR", Records = msg }, JsonRequestBehavior.AllowGet);
            }
        }
  • What is the code that calls this $.getJSON? Know if your problem is on the server or the client side?

  • The event is triggered by the keydown of the filter edit field. Each letter typed is to perform the search. I believe that’s a trait but I don’t know how to get around it. This behavior is presented in dev, I did not host the page, I believe that it should have the same behavior. If I type a different letter the first time the control is triggered, the more I present the memo problem if I type it again. I imagine the problem is on the client’s side.

1 answer

0

Hello

To be able to solve this problem. Find out after many searches that some browsers cache these filters, and to get around the problem it is necessary to do the following.

If you are using the $.ajax should be included the cache = false and to the $.getJSON one more parameter has to be included new Date().getTime(). This will make you always change query that is being passed to the controller by forcing the browser to perform the search.

Browser other questions tagged

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