jquery与Ajax
1,首先导入jquery.js文件不用我说了吧!
2,看代码:
$(function() {
$.ajax({
type: "post",
url: "Handler.ashx", //这里请求的是一个一般处理程序,也可以是页面!
async: false,
error: function() {
alert("数据库连接失败!");
},
success: function(response) {
var data = eval("(" + response + ")"); //这里接收到发送过来的数据,转成json对象,此时就有了对象属性了
var str = "";
for (var i = 0; i < data.length; i++) {
str += "编号:" + data[i].ID + " ";
str += "工程名称:" + data[i].ProjectName + "<br/>";
}
$("#div_ajax").html(str);
}
});
})
这下看看一般处理程序(Handler.ashx)里是怎么写的代码?
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string currentPage = context.Request["currentPage"].ToString();
List<Model.Technology_ConstructionOrganization_ReviewlModel> list = new List<Technology_ConstructionOrganization_ReviewlModel>();
if (!string.IsNullOrEmpty(currentPage))
list = this.getListByPage(Convert.ToInt32(currentPage)); //这里是拿数据
var Json = Newtonsoft.Json.JsonConvert.SerializeObject(list); //把拿到的数据转成json字符串
context.Response.Write(Json); //发送到客户端
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
//这里是你访问数据库后台代码(一般不介意写在这里,引用BLL层的方法去访问,这里方便读者,所以)
public List<Model.Technology_ConstructionOrganization_ReviewlModel> getListByPage(int currentPage)
{
List<Technology_ConstructionOrganization_ReviewlModel> list = new List<Technology_ConstructionOrganization_ReviewlModel>();
string ConnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
string sql = "select top 10 * from Technology_ConstructionOrganization_Reviewl where ID not in(select top (" + (currentPage - 1) * 10 + ") ID from Technology_ConstructionOrganization_Reviewl)";
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Technology_ConstructionOrganization_ReviewlModel model = new Technology_ConstructionOrganization_ReviewlModel();
model.ID = Convert.ToInt32(reader["ID"]);
model.EngineeringName = reader["EngineeringName"].ToString();
model.SchemeName = reader["SchemeName"].ToString();
model.ProjectName = reader["ProjectName"].ToString();
model.Numbers = reader["Numbers"].ToString();
model.ConstructionArea = reader["ConstructionArea"].ToString();
model.StructureForm = reader["StructureForm"].ToString();
model.Layer = reader["Layer"].ToString();
model.AuditPeopleName = reader["AuditPeopleName"].ToString();
model.ApprovalName = reader["ApprovalName"].ToString();
model.ExaminationApprovalContent = reader["ExaminationApprovalContent"].ToString();
list.Add(model);
}
}
}
return list;
}

浙公网安备 33010602011771号