通过HtmlHandler.ashx的页面处理程序执行返回数据:
代码
public void ProcessRequest (HttpContext context) {
int count = Convert.ToInt32(ConfigurationManager.AppSettings["DataCount"]);
List list = new List();
for (int i = 0; i < count; i++)
{
list.Add(new UserInfo { UserId = i, UserName = "leepy" + i, Email = "sunleepy" + i + "@gmail.com" });
}
StringBuilder builder = new StringBuilder();
for (int i = 0, length = list.Count; i < length; i++)
{
builder.AppendFormat("
UserId:{0}, UserName:{1}, Email:{2}
", list[i].UserId, list[i].UserName, list[i].Email);
}
context.Response.Write(builder.ToString());
}
int count = Convert.ToInt32(ConfigurationManager.AppSettings["DataCount"]);
List list = new List();
for (int i = 0; i < count; i++)
{
list.Add(new UserInfo { UserId = i, UserName = "leepy" + i, Email = "sunleepy" + i + "@gmail.com" });
}
StringBuilder builder = new StringBuilder();
for (int i = 0, length = list.Count; i < length; i++)
{
builder.AppendFormat("
UserId:{0}, UserName:{1}, Email:{2}
", list[i].UserId, list[i].UserName, list[i].Email);
}
context.Response.Write(builder.ToString());
}
运行加载结果为:

比WebService也快了一些。
3)原生Ajax调用返回Html字符串加载数据:
代码
function bindDataPrototypeAjaxHtml() {
var watch = new Stopwatch();
watch.start();
Request.sendGET("HtmlHandler.ashx", false, function(data) {
$("#msg6").html(data);
watch.stop();
$("#time6").html("用时:" + watch.ms + "毫秒.");
});
}
var watch = new Stopwatch();
watch.start();
Request.sendGET("HtmlHandler.ashx", false, function(data) {
$("#msg6").html(data);
watch.stop();
$("#time6").html("用时:" + watch.ms + "毫秒.");
});
}
运行加载结果为:

和jQuery的$.ajax函数也是基本相差无几。
测试说明:$.ajax,$.getJSON,原生对象返回返回Html字符串加载数据的效率基本一样,比WebService的效率好些;并且通过3和4的比较,说明通过在后台拼接Html字符串,比在前台来拼接的过程来得更高效。