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