异步请求(多种方式)

之前每次请求都会重新请求所有资源。但是很多时候仅仅需要页面的局部数据刷新即可。此时需要发送异步请求来实现这种局部数据刷新的要求。使用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);
			})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值