accp8.0转换教材第11章JAjax加护扩展理解与练习

时间:2023-03-09 06:02:41
accp8.0转换教材第11章JAjax加护扩展理解与练习

①杂记:前面有原生态JavaScript实现ajax这里又多了更简单的方法实现ajax

②$.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()方法实现异步验证注册邮箱

 $(function(){
$("#email").blur(function(){
var email=$("#email").val();
if(email==null || email==""){
$("#samp").html("邮箱不能为空!");
}
else{
$.get("userServlet","email="+email,callBack);
function callBack(data){
if(data=="true"){
$("#samp").html("邮箱已被注册!");
}
else{
$("#samp").html("邮箱可注册!");
}
}
}
}); });

2.使用$.getJSON()方法加载管理员页面主题列表

 $.getJSON("userServlet","por=top",callTopics);

     function callTopics(top){

         var $userul=$("#userul").empty();
for(var i=0;i<top.length;){
//alert("ddd");
$userul.append(
"<li>"+top[i].topics+"&nbsp;&nbsp;<a href=''>修改</a>&nbsp;&nbsp;<a href=''>删除</a></li>"
);
i++;
if(i==top.length){ break;
}
}
}

3.在Ajax中直接返回HTML内容生成主题管理页面

 $.ajax({
"url":"userServlet",
"type":"GET",
"data":"por=top1",
"dataType":"html",
"success":callTopics
});
function callTopics(data){
$("#userul").html(data);
}

4.使用.load()方法为管理员页面加载服务器生成的主题列表

$("#userul").load("userServlet","por=top1");

5.使用Ajax实现无刷新的新闻评论功能

 if(por.equals("addCom")){
//上机5添加评论
comment com=new comment();
commentdao comdao=new commentimpl();
String name=request.getParameter("cauthor1");
String ip=request.getParameter("cip");
String content=request.getParameter("ccontent");
String ctime="2017-7-4";
//time.toString();
com.setCname(name);
com.setCcontent(content);
com.setCip(ip);
com.setCtime(ctime);
int re=comdao.addcomment(com);
String result="";
if(re>0){
result="success";
}else {
result="添加失败!";
} response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.print("[{\"result\":\""+result+"\",\"ctime\":\""+ctime+"\"}]");
out.flush();
out.close(); }

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对象

欢迎提问,欢迎指错,欢迎讨论学习信息 有需要的私聊 发布评论即可 都能回复的

原文在博客园http://www.cnblogs.com/a782126844/有需要可以联系扣扣:2265682997