②$.get()方法的常用参数
参数 类型 说明
url string 必选,规定发送地址
data plainobject或string 可选,规定请求发送的数据
success function(plainobject data,
string textstatus,jqxhr jqxhr)
可选,成功后调用的函数
参数data:可选服务器返回结果
参数textstatus:可选描述请求状态
参数jqxhr:可选是xmlhttprequest的超集
(如果指定datatype这个必选)
datatype string 可选:预期服务器返回的数据类型
③$.post()方法的常用参数同上
一.单词部分(json常用单词)
1.load 加载 2.serialize序列化 3.contains 包含 4.feature 特征
5.quote 引用 6.skip 跳跃 7.transient 短暂的 8.pretty 相当
9.prototype 原型 10.conflict 冲突
二.关于json一些常见问题
1.jquery实现ajax的主要方法
①原生态实现
②$.get()和$.post()方法
③$.getjson()方法
④.load()
2.jquery解析表单数据
jquery的.serializearray()方法会从一组表单元素中检测有效控件:
①元素不能被禁用
②元素必须有name属性
③选中的checkbox才是有效的
④选中的radio才是有效的
⑤只有触发提交事件的submit按钮才是有效的
⑥file元素不会被序列化
3.jquery与其它3
三.实现ajax
1.使用$.get()方法实现异步验证注册邮箱
1 $(function(){ 2 $(#email).blur(function(){ 3 var email=$(#email).val(); 4 if(email==null || email==){ 5 $(#samp).html(邮箱不能为空!); 6 } 7 else{ 8 $.get(userservlet,email=+email,callback); 9 function callback(data){10 if(data==true){11 $(#samp).html(邮箱已被注册!);12 }13 else{14 $(#samp).html(邮箱可注册!);15 }16 }17 }18 }); 19 20 });
2.使用$.getjson()方法加载管理员页面主题列表
1 $.getjson(userservlet,por=top,calltopics); 2 3 function calltopics(top){ 4 5 var $userul=$(#userul).empty(); 6 for(var i=0;i<top.length;){ 7 //alert(ddd); 8 $userul.append( 9 <li>+top[i].topics+ <a href=''>修改</a> <a href=''>删除</a></li>10 );11 i++;12 if(i==top.length){13 14 break;15 }16 }17 }
3.在ajax中直接返回html内容生成主题管理页面
1 $.ajax({ 2 url:userservlet, 3 type:get, 4 data:por=top1, 5 datatype:html, 6 success:calltopics 7 }); 8 function calltopics(data){ 9 $(#userul).html(data);10 }
4.使用.load()方法为管理员页面加载服务器生成的主题列表
$(#userul).load(userservlet,por=top1);
5.使用ajax实现无刷新的新闻评论功能
1 if(por.equals(addcom)){ 2 //上机5添加评论 3 comment com=new comment(); 4 commentdao comdao=new commentimpl(); 5 string name=request.getparameter(cauthor1); 6 string ip=request.getparameter(cip); 7 string content=request.getparameter(ccontent); 8 string ctime=2017-7-4; 9 //time.tostring();10 com.setcname(name);11 com.setccontent(content);12 com.setcip(ip);13 com.setctime(ctime);14 int re=comdao.addcomment(com);15 string result=;16 if(re>0){17 result=success;18 }else {19 result=添加失败!;20 }21 22 23 response.setcontenttype(text/html;charset=utf-8);24 printwriter out=response.getwriter();25 out.print([{\result\:\+result+\,\ctime\:\+ctime+\}]);26 out.flush();27 out.close();28 29 }
6.使用fastjson改造管理员页面加载主题列表
topdao nd=new topimpl(); list<top> listtop=nd.alltop(); string titlejson=json.tojsonstringwithdateformat(listtop,yyyy-mm--dd hh:mm:ss);
四.加深理解
通过fastjson的相关api可以简化服务器端生成的json字符串代码
$.parsejson()方法用来将json格式字符串解析为json对象
以上就是accp8.0转换教材第11章ajax交互扩展理解与练习的详细内容。
