c# - ASP.Net Web 服务中的 JQUery 自动完成

标签 c# jquery asp.net jquery-autocomplete

这让我发疯,我只是不断收到“错误”消息,没有其他消息。我使用 AJAX Toolkit 使用此自动完成功能,但我想尝试 JQuery,我对 JQuery 的经验很少。这是WebService代码:

  [WebService(Namespace = "http://tempuri.org/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  [System.Web.Script.Services.ScriptService]
 public class WebService : System.Web.Services.WebService {

public WebService () {

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
}

[WebMethod]
public static string GetNames(string prefixText, int count)
{
    Trie ArtistTrie = new Trie();
    if (HttpContext.Current.Cache["CustomersTrie"] == null)
    {          
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "SELECT * FROM TopArtists ORDER BY name ASC";
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();            
        da.SelectCommand = comm;
        comm.Connection = conn;
        conn.Open();
        da.Fill(dt);
        conn.Close();
        Trie newtrie = new Trie();
        foreach (DataRow dr in dt.Rows)
        {
            // topartists.Add(dr[0].ToString());
            newtrie.Add(dr[0].ToString());
        }
        HttpContext.Current.Cache["CustomersTrie"] = newtrie;
    }
     ArtistTrie = (Trie)HttpContext.Current.Cache["CustomersTrie"];

     List<string> list = ArtistTrie.GetCompletionList(prefixText, 10);
     List<Band> list1 = new List<Band>();
     foreach (string a in list)
     {
         Band newband = new Band();
         newband.Name = a;
         list1.Add(newband);
     }
     string json = JsonConvert.SerializeObject(list1, Formatting.Indented);
     return json;

}

这是 JQuery 代码:

    <script type="text/javascript">
      $(document).ready(function () {
        $(function () {
           $("#tb1").autocomplete({

        source: function (request, response) {
            $.ajax({
                url: "WebService.asmx/GetNames",
                data: request.term ,
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",

                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 2
         });
        });
      }) 
    </script>

最佳答案

首先,您的 jQuery 代码有错误,包括末尾缺少分号以及包装自动完成的不必要的函数,请尝试:

<script type="text/javascript">
    $(document).ready(function() {
        $("#tb1").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: "WebService.asmx/GetNames",
                    data: request.term,
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 2
        });
    }); 
</script>

关于c# - ASP.Net Web 服务中的 JQUery 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10660389/

相关文章:

c# - 在 .NET MVC 应用程序中验证 Azure AD B2C JWT

c# - P/Invoke 声明不应是安全关键的

jquery - Fancybox iframe 类型关闭时返回值

c# - 如何安排日期时间列表的特殊表格格式

c# - 您知道从 XSD 创建 C++ 或 C# 类(数据模型)的优秀免费软件吗?

javascript - 使用文本输入字段时出现 Javascript 中的 NaN

javascript - 将响应式自定义类添加到移动菜单

html - ASP 复选框长文本未对齐

c# - 从基类继承 href 和 id 的 ASP.NET WebApi 响应模型中的排序属性

c# - 使用引用值作为参数,带或不带 "ref"?