62,046
社区成员
发帖
与我相关
我的任务
分享
//获取ID
//查询数据放到list
string s = string.Empty;
if (find_list.Count > 0)
{
s = s + "{\"x\":[";
foreach (tb_content cf in find_list)
{
s = s + "{\"Id\":\"" + cf.C_ID + "\",\"Name\":\"" + cf.Title + "\"},";
}
s = s + "]};";
s = s.Substring(0, s.Length - 4) + s.Substring(s.Length - 3, 3);//把最后一个记录{}后的逗号","去掉
}
else
{
s = "暂无数据";
}
context.Response.Write(s);
<script src="Js/jquery-1.4.4.js" type="text/javascript"></script>
<%--<script src="Js/jquery-1.5.min.js" type="text/javascript"></script>--%>
<script type="text/javascript">
function f(v) {
$.getJSON(
"ho.ashx?id=" + v + "&" + Math.random(),
function(data) {
alert("aaa");
alert(data.x.length);
}
);
}
</script>
<%@ Page Language="C#" %>
<!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">
<script src="jquery-1.4.4.min.js"></script>
<script>
function f(v) {
$.getJSON(
"ho.ashx?id=" + v + "&" + Math.random(),
function(data) {
alert("aaa");
alert(data.x.length);
}
);
}
f("ok");
</script>
</head>
<body>
<form id="form1" runat="server">
</form>
</body>
</html>
<%@ WebHandler Language="C#" Class="ho" %>
using System;
using System.Web;
public class ho : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Charset = "utf-8";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("{\"x\":"
+ "["
+ "{\"Id\":\"11\",\"Name\":\"美国十六天游标准行程\"},"
+ "{\"Id\":\"12\",\"Name\":\"德法荷比卢奥瑞捷意梵十国十四日游\"},"
+ "{\"Id\":\"49\",\"Name\":\"英国瑞士八日游\"},"
+ "{\"Id\":\"50\",\"Name\":\"非凡泰新马升级版9晚11天\"}"
+ "]}");
context.Response.Write(sb.ToString());
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url:"Handler.ashx",
type:"get",
data:"",
dataType:"json",
success:function(result){
$.each(result.x,function(i){
alert(result.x[i].Id+"--"+result.x[i].Name)
})
}
})
})
</script>
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/plain";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("{x:"
+ "["
+ "{\"Id\":\"11\",\"Name\":\"美国十六天游标准行程\"},"
+ "{\"Id\":\"12\",\"Name\":\"德法荷比卢奥瑞捷意梵十国十四日游\"},"
+ "{\"Id\":\"49\",\"Name\":\"英国瑞士八日游\"},"
+ "{\"Id\":\"50\",\"Name\":\"非凡泰新马升级版9晚11天\"}"
+ "]}");
context.Response.Write(sb.ToString());
context.Response.End();
}