<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script>
$(function () {
$.ajax({
url: "Handler.ashx",
dataType: "json",
cache: false,
data: null,
type: "GET",
success: function (data) {
var msg = "";
for (var i = 0, length = data.length; i < length; i++) {
msg += "<Div style='color:red'>UserName:" + data[i].UserName + ",Telephone:" + data[i].Telephone + "</div>";
}
alert(msg);
}
});
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
System.Collections.Generic.List<User> list = new System.Collections.Generic.List<User>();
for (int i = 0; i < 10; i++)
{
list.Add(new User { UserName = "zzl" + i, Tel = "13521972991" });
}
string str = "[";
list.ForEach(i =>
{
str += "{" + string.Format("\"UserName\":\"{0}\", \"Telephone\":\"{1}\"", i.UserName, i.Tel) + "},";
});
str = str.TrimEnd(',') + "]";
context.Response.Write(str);
}
public bool IsReusable {
get {
return false;
}
}
}
class User
{
public string UserName { get; set; }
public string Tel { get; set; }
}