2013-10-31 37 views
1

下面是ASPX代碼:jQuery的自動完成功能成功處理程序不調用

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestingJqueryAutoComplete.aspx.cs" 
Inherits="ProjectForTestingPurpose.TestingJqueryAutoComplete" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script> 
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script> 

<style type="text/css"> 
     * 
     { 
      margin:0; padding:0; 
     } 
</style> 

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

     $("#txtCities").autocomplete({ 
      minLength: 2, 
      source: function (request, response) { 
       $.ajax({ 
        type: "POST", 
        dataType: "json", 
        url: "/GenericAjaxHandler.ashx", 
        data: { text: request.term, type: "requestcities" } 
       }); //ajax 
      }, //source 
      success: function (data) { 
       debugger; 
       alert(data); 
       response($.map(data, function (item) { 
        return { 
         label: item.name, 
         value: item.name 
        } 
       } 
        )//function 
       ); //response 
      }, //success 
      error: function (response) { 
       debugger; 
       alert(response.responseText); 
      }, 
      failure: function (response) { 
       debugger; 
       alert(response.responseText); 
      }, 
      select: function (event, ui) { 
       alert("hello"); 
      } 

     }); // autocomplete 

    });   // document ready 
</script> 

城市

服務器端代碼。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ProjectForTestingPurpose 
{ 
    /// <summary> 
    /// Summary description for GenericAjaxHandler 
    /// </summary> 
    public class GenericAjaxHandler : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      context.Response.ContentType = "application/json"; 

      if (context.Request["type"].ToString().ToLower() == "requestcities") 
      { 
       string response = string.Empty; 

       response = "[{\"name\":\"Islamabad\"},{\"name\":\"Lahore\"},{\"name\":\"Karachi\"}]"; 

       context.Response.Write(response); 
      } 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 
    } 
} 

我不知道jquery自動完成的成功處理程序沒有被調用的問題。

+0

檢查錯誤控制檯。你看到其他處理程序發出「警報」了嗎? –

+0

是的,我檢查控制檯和沒有被調用的處理程序。 –

回答

0

看起來它不是自動完成處理程序的問題。可能是您創建的JSON格式不正確。 避免這一點,你必須創建一個Genric名單收集和序列化此彙集成JSON

代碼示例

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ProjectForTestingPurpose 
{ 
    /// <summary> 
    /// Summary description for GenericAjaxHandler 
    /// </summary> 
    public class GenericAjaxHandler : IHttpHandler 
    { 

     public void ProcessRequest(HttpContext context) 
     { 
      context.Response.ContentType = "application/json"; 

      if (context.Request["type"].ToString().ToLower() == "requestcities") 
      { 
       string response = string.Empty; 

       List<City> lstCities = new List<>(); 

       City city = new City(); 
       city.Name = "Islamabad"; 
       lstCities.Add(city); 

       city = new City(); 
       city.Name = "Lahore"; 
       lstCities.Add(city); 

       city = new City(); 
       city.Name = "Karachi"; 
       lstCities.Add(city); 

       JavaScriptSerializer serializer = new JavaScriptSerializer(); 
       response = serializer.serialize(lstCities) 

       context.Response.Write(response); 
      } 
     } 

     public bool IsReusable 
     { 
      get 
      { 
       return false; 
      } 
     } 
    } 

    public class City 
    { 
     private string _name; 

     public City() 
     { 
     } 

     public string Name 
     { 
     get; 
     set; 
     } 
    } 
}