Ciao a tutti
vorrei inserire in una pagina l'autocomplete di jquery caricando i dati con ajax.
Ho fatto così:
$(function() {
$(".auto-city").autocomplete({
source: function(request, response) {
$.ajax({
url: "/Handler/ajaxAutocomplete.ashx",
data: { "type" : "citta", "q": request.term },
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function(data) { return data; },
success: function(data) {
response($.map(data.d, function(item) {
return {
value: item.Comune
}
}))
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
},
minLength: 2
});
});
<%@ WebHandler Language="C#" Class="ajaxAutocomplete" %>
using System;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
public class ajaxAutocomplete : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Expires = -1;
string result = string.Empty;
string type = context.Request["type"];
string q = string.Empty;
q = context.Request.Form["term"].Trim();
JavaScriptSerializer ser = new JavaScriptSerializer();
switch (type)
{
case "citta":
List<tipoComune> comuni = CittaProvinceNazioni.CittaProvincia(string.Empty);
result = ser.Serialize(comuni.Where(item => item.Comune.ToLower().StartsWith(q.ToLower())));
break;
default:
break;
}
context.Response.ContentType = "text/plain";
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
private void LogRequest(string Log)
{
//StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("/Log") + "\\Log.txt", true);
//sw.WriteLine(DateTime.Now.ToString() + " - " + Log);
//sw.Flush();
//sw.Close();
}
}
Il problema è che non riesco a passare i parametri al file ashx.
q = context.Request.Form["term"].Trim() mi restituisce null.
Dove sbaglio?
Grazie mille