ajax入门基础
一、简介
AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。
AJAX 是一种用于创建快速动态网页的技术。
AJAX通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新。
AJAX 不是新的编程语言,而是一种使用现有标准的新方法。
AJAX 最大的优点是在不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容。
AJAX 不需要任何浏览器插件,但需要用户允许JavaScript在浏览器上执行。
二、基本结构
1、新建Linq(数据库信息封装绑定)
2、组键json对象(数据库数据加载到后台)
对象格式:"{"key":"value","key":"value"}"
数组格式:"[{"key":"value"},{},{}]"
例:
using System; using System.Web; using System.Text; using System.Linq; using System.Collections.Generic; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { StringBuilder str = new StringBuilder(); str.Append("["); //string s = context.Request["id"]; int count = 0; using (StudentDataContext con = new StudentDataContext()) { List<Users> ulist = new List<Users>(); ulist = con.Users.ToList(); foreach (Users us in ulist) { count++; if (count < ulist.Count) { str.Append("{"ids":"" + us.Ids + "","username":"" + us.Username + "","password":"" + us.Password + "","nickname":"" + us.Nikename + "","sex":"" + us.Sex + "","birthday":"" + us.Birthday + "","nation":"" + us.Nation + "","num":"" + us.Ids + ""},"); } else { str.Append("{"ids":"" + us.Ids + "","username":"" + us.Username + "","password":"" + us.Password + "","nickname":"" + us.Nikename + "","sex":"" + us.Sex + "","birthday":"" + us.Birthday + "","nation":"" + us.Nation + "","num":"" + us.Ids + ""}"); } } } str.Append("]"); context.Response.Write(str); context.Response.End(); } public bool IsReusable { get { return false; } } }
3、js页面数据展示(后台与前台的数据交互)
table id="tb1" style=" text-align: center; 100%;"> 2 <thead> 3 <tr style="color: #ff6a00;"> 4 <td>用户名</td> 5 <td>密码</td> 6 <td>昵称</td> 7 <td>性别</td> 8 <td>生日</td> 9 <td>年龄</td> 10 <td>民族</td> 11 </tr> 12 </thead> 13 <tbody> 14 </tbody> 15 </table> 16 <input type="button" value="加载" id="btn1" /> 复制代码 复制代码