如何在自动完成中使用处理程序

时间:2016-03-02 12:53:09

标签: javascript asp.net

  $(".productspecification").autocomplete({
          source: '/static/components/autocomplete/listspecification.ashx'
    });
  

这是我的处理程序

   public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/javascript";
    StringBuilder sbSpecs = new StringBuilder();
    string[] lines = System.IO.File.ReadAllLines(HttpContext.Current.Server.MapPath("~/YKIOSK/static/components/autocomplete/specifications.txt"));
    ArrayList result = new ArrayList();
    foreach (string s in lines)
    {
        if (s.ToLower().Contains(context.Request.QueryString["term"].ToLower()))
        {
            result.Add(s); 
        } 
    }
    context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(result));
}

我想添加一个webservices来代替specifications.txt

2 个答案:

答案 0 :(得分:0)

您需要使用ajax来获取数据,然后您需要创建自动完成的项目。

$("#productspecification").autocomplete({
    source: function (request, response) {
         $.ajax({
             url: "/static/components/autocomplete/listspecification.ashx",
             type: "GET",
             data: request,
             success: function (data) {
                 response($.map(data, function (el) {
                     return {
                         label: yourLabel,
                         value: Yourvalue
                     };
                 }));
             }
         });
    },

然后寻找,如何访问Web服务c#,asp.net。

//Use the Web Service that your Web server provides.
       localhost.Service1 MyService = new localhost.Service1();
       //Invoke the public WebMethod of the serice.
var webdata= MyService.MyMethod();

或使用web clienthttp client

Using Client As New HttpClient()
Dim APIhostUri As String = “http://localhost:2025/”
Dim APIResponse As HttpResponseMessage
Client.BaseAddress = New Uri(APIhostUri)

答案 1 :(得分:0)

首先,我认为您应该放弃处理程序并从脚本中调用webservice以获取此类自动填充数据

$("#productspecification").autocomplete({
source: function (request, response) {
     $.ajax({
         url: "url to your web method",
         type: "GET",
         data: request,
         success: function (data) {
             response($.map(data, function (el) {
                 return {
                     label: yourLabel,
                     value: Yourvalue
                 };
             }));
         }
     });
},

如果您仍想使用处理程序,则需要插入Web服务使用者模块以从Web服务获取数据并作为响应返回。

像这样。

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/javascript";
StringBuilder sbSpecs = new StringBuilder();
var wcfClient=new YourWcfProxy();
var term=context.Request.QueryString["term"];
string[] lines = wcfClient.GetLinesFor(term);
context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(result));
}