JQuey中的Ajax:
三层:$—>JQuery对象
最底层:$.ajax();
中间层:$.load('url',data,callback)
$.get(url,data,callback,type) :callback 回调函数 type 回调函数指定接收数据的返回类型
$.post(url,data,callback,type)
最上层:$.getJSON();
$.load('url',data,callback) 的学习:
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=path %>/js/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function(){
alert('----------');
$("#btnLoad").click(function(){
$("#divComment").load("<%=path%>/jsp/Comment.html");
});
});
</script>
</head>
<body>
<h1>楼主:快来看啦,外星人来了</h1>
<input type="button" value="加载评论" id="btnLoad">
<div id="divComment" style="border: 1px groove red; "></div>
</body>
</html>
/js/jquery-1.4.1.js
/jsp/Comment.html:
<!DOCTYPE html>
<html>
<head>
<title>Comment.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<div id="comment">
<ul>
<li>发言人:<a>李四</a></li>
<div>沙发</div>
<li>发言人:<a>王五</a></li>
<div>板凳</div>
<li>发言人:<a>李四</a></li>
<div class="selLoad">哈哈,不错,很精彩</div>
</ul>
</div>
</body>
</html>
$.get(url,data,callback,type)、$.post(url,data,callback,type) 、$.getJSON();的学习:
index.jsp:
<script type="text/javascript" src="<%=path %>/js/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function(){
$("#btnLoad").click(function(){
$.post("demo",{usName:$("#usName").val(),message:$("#mssage").val()},function(data,textStatus){
//$("#divComment").append(data);
//遍历data集合数据
$.each(data,function(property,item){alert(item.usName+":"+item.msg);});
//$.each(data,function(property,item){alert(property+":"+item);});
},"json");
});
$("#btnLoad").click(function(){
$.getJSON("student.json",function(data){
$.each(data,function(i,item){alert(item.usName+" "+item.usSex+" "+item.usAge);});
});
});
</script>
</head>
<body>
<h1>楼主:快来看啦,外星人来了</h1>
<input type="button" value="加载评论" id="btnLoad">
游客<input type="text" size="10" id="usName"/>说话:<input type="text" size="20" id="mssage"/>
<div id="divComment" style="border: 1px groove red; "></div>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<servlet>
<servlet-name>AjaxDemo</servlet-name>
<servlet-class>com.zuxia.yc42.servlet.AjaxDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxDemo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
——public class AjaxDemo extends HttpServlet :
package com.zuxia.yc42.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;
import net.sf.json.util.JSONStringer;
import com.zuxia.yc42.po.UserInfo;
public class AjaxDemo extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
response.setCharacterEncoding("UTF-8");
response.setContentType("");
System.out.println(request.getContentType()+"\t"+request.getCharacterEncoding());
String usName = null;
String msg = null;
try {
usName = new String((request.getParameter("usName").getBytes("ISO-8859-1")),"UTF-8");
msg = new String((request.getParameter("message").getBytes("ISO-8859-1")),"UTF-8");
List<UserInfo> list =new ArrayList<UserInfo>();
UserInfo userInfo = new UserInfo();
userInfo.setUsName(usName);
userInfo.setMsg(msg);
list.add(userInfo);
String jsonString ="{'usName':'aaa','msg':'TTTTTTTT'}";
responseOutWithJson(response,list);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
{
System.out.println("doGet方法被调用");
doPost(request,response);
}
/**
* 以JSON格式输出
* @param response
*/
protected void responseOutWithJson(HttpServletResponse response,
Object responseObject) {
//将实体对象转换为JSON Object转换
//JSONObject responseJSONObject = JSONObject.fromObject(responseObject);
JSONArray responseJSONObject = JSONArray.fromObject(responseObject);
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=UTF-8");
PrintWriter out = null;
try
{
out = response.getWriter();
out.append(responseJSONObject.toString());
System.out.println(responseJSONObject.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
}
com.zuxia.yc42.po
——public class UserInfo:
package com.zuxia.yc42.po;
public class UserInfo
{
private String usName;
private String msg;
public String getUsName() {
return usName;
}
public void setUsName(String usName) {
this.usName = usName;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
WebRoot
——student.json:
[
{
"usName":"xiaoming",
"usSex":"nan",
"usAge":"22"
},
{
"usName":"xiaoli",
"usSex":"nv",
"usAge":"18"
}
]
以下是index.jsp页面代码集合:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="<%=path %>/js/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(function(){
// alert('----------');
// $("#btnLoad").click(function(){
// $("#divComment").load("<%=path%>/jsp/Comment.html");
// });
$("#btnLoad").click(function(){
$.post("demo",{usName:$("#usName").val(),message:$("#mssage").val()},function(data,textStatus){
//$("#divComment").append(data);
//遍历data集合数据
$.each(data,function(property,item){alert(item.usName+":"+item.msg);});
//$.each(data,function(property,item){alert(property+":"+item);});
},"json");
});
$("#btnLoad").click(function(){
$.getJSON("student.json",function(data){
$.each(data,function(i,item){alert(item.usName+" "+item.usSex+" "+item.usAge);});
});
});
});
</script>
</head>
<body>
<h1>楼主:快来看啦,外星人来了</h1>
<input type="button" value="加载评论" id="btnLoad">
游客<input type="text" size="10" id="usName"/>说话:<input type="text" size="20" id="mssage"/>
<div id="divComment" style="border: 1px groove red; "></div>
</body>
</html>
有需要该JQueryAjax使用到的jar包的可以去我的资源页下载~