前台调用
url="Handler1.ashx";
$.ajax({
type: "post",
url: url,
data: { "stype": stype, "cityName": escape(cityName), "aqi": aqi },
dataType: "json", //"application/json", //
success: function (data, state) {
lat = []; lon = [];
var d = new Date();
var time = d.getDate();
var companyList = "";
var onLine = 0;
var unLine = 0;
var total = 0;
if (data != "") {
var aad = eval(data);
$.each(aad, function (i, item) { //返回sname,Longitude,Latitude,did,isMonitor,stype,city_name,sid,Udate,AQILevel,AQI,aqiTime,sno
var id = i + 1;
companyList += CreateCompanyList(item, id);
lat.push(item.Latitude);
lon.push(item.Longitude);
onLine++;
});
total = aad.length;
if (pointType == "selected") {//高亮显示
ShowHighLight(aad);
}
else drawPoint(aad); //在地图上标注查询结果
}
companyList += "</table>";
SetCompanyList(companyList);
SetCompanyState(total, onLine, unLine);
},
complete: function (aa, bb) {
arr = aa;
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
几个关键参数解释:
data: 传到后台的参数。
url : 后台地址,即Handler1.ashx文件的地址
dataType: 返回值的类型。如果此参数与后台返回值类型不匹配,会报字符或语法错误
后台方法ASHX文件:
<span style="font-size:18px;"> /// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/json";// "application/vnd.ms-excel";// "text/plain";//
string stype="",cityName="",aqi="";
if (context.Request["stype"] != null)
{
stype = context.Request["stype"].ToString();
}
if (context.Request["cityName"] != null)
{
cityName = HttpUtility.UrlDecode(context.Request["cityName"]);
}
if (context.Request["aqi"] != null)
{
aqi = context.Request["aqi"].ToString();
}
// List<Air_nodeInfo> nodeList = GetAir_nodeListAll();
DataTable names = GetNodeList(stype, cityName, aqi);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonEmp = Serialize(names,false);
context.Response.Write(jsonEmp);
}
public DataTable GetNodeList(string stype, string cityName, string aqi)
{
DataTable dt = new DataTable();
Air_nodeService nodeService = new Air_nodeService();
dt = nodeService.GetNodeList(stype, cityName, aqi);//sname,Longitude,Latitude,did,isMonitor,stype,city_name,sid,Udate,AQILevel,AQI,aqiTime,sno
return dt;
}
public List<Air_nodeInfo> GetAir_nodeListAll()
{
List<Air_nodeInfo> nodeList = new List<Air_nodeInfo>();
Air_nodeService nodeService = new Air_nodeService();
nodeList = nodeService.GetAir_nodeListAll();
return nodeList;
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>序列化方法
/// 不需要分页
/// </summary>
/// <param name="dt"></param>
/// <param name="flag">false</param>
/// <returns></returns>
public string Serialize(DataTable dt, bool flag)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
return serializer.Serialize(list); ;
}
/// <summary>序列化方法
/// 不需要分页
/// </summary>
/// <param name="dt"></param>
/// <param name="flag">false</param>
/// <returns></returns>
public string Serialize(DataSet ds, bool flag)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
ArrayList listss = new ArrayList();
List<Dictionary<string, object>> list ;
DataTable[] dt = new DataTable[]{ds.Tables[0],ds.Tables[1]};
for (int i=0;i< dt.Length;i++)
{
list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt[i].Rows)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt[i].Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
listss.Add(list);
}
return serializer.Serialize(listss);
}
}</span>