How to use Jquery autocomplete with C# ASP.Net?

Asked

Viewed 136 times

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.

Erro que eu obtenho no console do navegador depois de digitar uma letra no textbox

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:

  • 1

    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).

  • Instead of using a trow take the exception text inside the catch and show us so that we have a more detailed idea of what is happening.

  • 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. :(

1 answer

0

within ~/App_start/Routeconfig.Cs change:

settings.AutoRedirectMode = RedirectMode.Permanent;

To:

settings.AutoRedirectMode = RedirectMode.Off;

Or just comment on the line

If friendly Urls are active you should make this change

url: "Teste.aspx/GetAll",

To:

url: '<%= ResolveUrl("Teste.aspx/GetAll") %>'

I hope it helps.

Browser other questions tagged

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