0
Guys, I’m trying to use Jquery autocomplete in C# from a method that brings me a list of database clients (the database is the Northwind provided by Microsoft), but I’m getting an error that I can’t solve hours ago.
Below the code on the back end and also on the front end:
[WebMethod]
public List<Customer> GetAll(string nome)
{
SqlConnection connection;
SqlCommand command;
List<Customer> customersList = new List<Customer>();
SqlDataReader dataReader;
connection = new SqlConnection();
connection.ConnectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT * FROM Customers WHERE ContactName LIKE @nome";
command.Parameters.AddWithValue("@nome", "%" + nome + "%");
command.CommandType = CommandType.Text;
try
{
connection.Open();
dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Customer customer = new Customer();
customer.CustermerID = dataReader["CustomerID"].ToString();
customer.Name = dataReader["ContactName"].ToString();
customersList.Add(customer);
}
return customersList;
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Teste.aspx.cs" Inherits="JQueryAutocomplete.Teste" %>
<link type="text/css" href="Content/jquery-ui.min.css" rel="stylesheet" />
<script type="text/javascript" src="Scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
SearchText();
});
function SearchText() {
$("#nomeTextBox").autocomplete({
source: function(request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Teste.aspx/GetAll",
data: "{'nome':'" + document.getElementById('nomeTextBox').value + "'}",
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(result) {
alert("No Match");
}
});
}
});
}
</script>
Name:
It seems to me that your problem is not even in jQuery itself, but in which the server is not authorizing the request (error 401).
– Sam
Instead of using a
trow
take the exception text inside thecatch
and show us so that we have a more detailed idea of what is happening.– Augusto Vasques
The program does not fall in the catch block. What happens is that after I load the application and type some letter in the text box, instead of it showing me the filtered list with that particular digit, nothing happens and in the console appears error 401. But I don’t know, why is it causing this. :(
– Paulo Silas