Error while doing autocomplete c #

0

I'm trying to do an autocomplete, but it's giving the following error, not even doing the post ...

Mycodelookslikethis:

<scripttype="text/javascript">
$(function () {
    $("[id$=DsValor]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '@Url.Action("GetPalavraChave", "HomeCanal")',
                data: "{ 'prefixo': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('-')[0],
                            val: item.split('-')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("[id$=hfCustomerId]").val(i.item.val);
        },
        minLength: 1
    });
});

And the backend looks like this:

 [WebMethod]
    public static string[] GetPalavraChave(string prefixo)
    {
        List<string> clientes = new List<string>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["WhiteLabelVOD"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT * FROM Palavra_Chave WHERE Ic_Ativo = 1 AND Ds_Palavra_Chave LIKE @Texto + '%'";
                cmd.Parameters.AddWithValue("@Texto", prefixo);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        clientes.Add(string.Format("{0}-{1}", sdr["ContactName"], sdr["CustomerId"]));
                    }
                }
                conn.Close();
            }
        }
        return clientes.ToArray();
    }
    
asked by anonymous 15.02.2018 / 01:00

1 answer

0

I was able to solve it as follows, returning in Json!

   [HttpPost]
    public JsonResult AutoCompleteTag(string prefixo)
    {
        List<PalavraChaveModel> palavras = new List<PalavraChaveModel>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["WhiteLabelVOD"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT * FROM Palavra_Chave WHERE Ic_Ativo = 1 AND Ds_Palavra_Chave LIKE @Texto + '%'";
                cmd.Parameters.AddWithValue("@Texto", prefixo);
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        PalavraChaveModel pl = new PalavraChaveModel();
                        pl.IdPalavraChave = (Int32)sdr["Id_Palavra_Chave"];
                        pl.DsPalavraChave = (string)sdr["Ds_Palavra_Chave"];
                        palavras.Add(pl);
                    }
                }
                conn.Close();
            }
        }
        return Json(palavras, JsonRequestBehavior.AllowGet);
    }

And my view looks like this:

  $("[id$=DsValor]").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("AutoCompleteTag", "HomeCanal")',
                        data: "{'prefixo':'" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return { label: item.DsPalavraChave, value: item.DsPalavraChave, IdPalavraChave: item.IdPalavraChave };
                            }))

                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                select: function (e, i) {
                    $("#IdPalavraChave").val(i.item.IdPalavraChave);
                },
                minLength: 1
            });
    
16.02.2018 / 17:28