从后台调用数据源,转化为JSON字符串再将得到的JSON数据绑定到一个Table中。 首先用ASHX后缀名的文件讲数据从数据库中捞出来,转化为JSON字符串。 <%@ WebHandler Language="C#" Class="BidPriceDetail" %> using System; using System.Web; using System.Data; using System.Text; using MyStudy.DAL; public class BidPriceDetail : IHttpHandler, System.Web.SessionState.IReadOnlySessionState { #region IHttpHandler 成员 bool IHttpHandler.IsReusable { get { return false ; } } void IHttpHandler.ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; //注意ContentType的格式 string json = this.GetJsonStr(); context.Response.Write(json); } #endregion public string GetJsonStr() { string sql = "select * from target"; DAO dao = new DAO(); DataTable dt = dao.GetData(sql); StringBuilder json = new StringBuilder(); json.Append("{/"success/":["); foreach (DataRow row in dt.Rows) { json.Append("["); json.Append("/"" + row["code"].ToString().Trim() + "/","); json.Append("/"" + row["name"].ToString().Trim() + "/","); json.Append("/"" + row["description"].ToString().Trim() + "/","); json.Append("/"" + row["startprice"].ToString().Trim() + "/""); json.Append("],"); } json.Remove(json.Length - 1, 1); json.Append("]}"); return json.ToString(); } } 用JQuery去接收数据 function getdatabyashx() { $.ajax({ type: "POST", url: "TestJson.ashx", cache:false, success:function(data){ try{ var json = eval("("+data+")"); var allPrice = json.success; alert(allPrice); //这里就可以操作得到的JSON对象了 } catch(ex){ alert(data); } } }); } PageLoad的时候调用的JS函数 $(document).ready(function(){ getdatabyashx(); })