之前每次请求都会重新请求所有资源。但是很多时候仅仅需要页面的局部数据刷新即可。此时需要发送异步请求来实现这种局部数据刷新的要求。使用JSON数据传输格式。
取得一个雇员信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page isELIgnored="false"%>
<%
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 'demo.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">
<script type="text/javascript" src="js/jQuery.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
$(function(){
$("a").click(function(){
$.get("emp/getOne",{"id":7654},function(data){
alert(data);
})
})
})
</script>
</head>
<body>
<a href="javascript:void(0)">取得一个雇员信息</a>
</body>
</html>
String pathInfo = req.getPathInfo();
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
try {
if("/login".equals(pathInfo)){
this.login(req, resp);
}else if ("/regist".equals(pathInfo)) {
this.regist(req, resp);
}else if ("/remove".equals(pathInfo)) {
this.removeById(req, resp);
}else if ("/getOne".equals(pathInfo)) {
this.getEmpById(req,resp);
}else if ("/list".equals(pathInfo)) {
this.getEmpList(req,resp);
}
} catch (Exception e) {
e.printStackTrace();
}
取得多个信息
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page isELIgnored="false"%>
<%
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 'demo.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">
<script type="text/javascript" src="js/jQuery.js"></script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
$(function(){
$("a:eq(0)").click(function(){
$.get("emp/getOne",{"id":7654},function(data){
alert(data);
})
})
$("a:eq(1)").click(function(){
$.get("emp/list",{"kw":"","cp":1,"ls":10},function(data){
alert(data);
})
})
})
</script>
</head>
<body>
<a href="javascript:void(0)">取得一个雇员信息</a>
<a href="javascript:void(0)">取得多个雇员信息</a>
</body>
</html>
其他方式
$(function(){
$("a:eq(0)").click(function(){
$.get("emp/getOne",{"id":7654},function(data){
alert(data.empno);
},"json")
})
$(function(){
$("a:eq(0)").click(function(){
$.getJSON("emp/getOne",{"id":7654},function(data){
alert(data.empno);
})
})
$("a:eq(1)").click(function(){
$.post("emp/list",{"kw":"","cp":1,"ls":10},function(data){
alert(data);
})
})
ajax异步请求
$("a:eq(1)").click(function(){
$.ajax({
type:"post",
url:"emp/list",
data:"cp=1&ls=10&kw=A",
dataType:"JSON",
success:function(data){
alert(data)
}
})
})
设置同步
async:false,
锁定浏览器,只有请求处理完毕之后才能执行后面的代码
$(function(){
var emp;
$("a:eq(0)").click(function(){
$.ajax({
type:"post",
url:"emp/getOne",
data:"id=7654",
dataType:"JSON",
async:false,
success:function(data){
emp=data;
alert(emp.ename);
}
})
alert(emp.empno);
})