<%@ page language="java" import="java.util.*" pageEncoding="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 'ajax.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="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function(){
$('#username').blur(function(){
$.ajax({
type:'post',
dataType:'json',//服务端返回到客户端的数据格式, text/html
//对应checkName.action中request.getParameter("username")
data:{'username':$('#username').val()},
url:'${pageContext.request.contextPath }/checkName.action',
success:function(map){
$('#sp').html(map.info);
},
error:function(err){
$('#sp').html(err.responseText);
}
});
});
$('#username2').blur(function(){
var jsonObj = JSON.stringify({'userName':$('#username2').val()});
$.ajax({
type:'post',
dataType:'json',
contentType :'application/json', //发送信息至服务器时内容编码类型。
data:jsonObj,
url:'${pageContext.request.contextPath }/checkNameJson.action',
success:function(userinfo){
$('#sp2').html("正确的消息:"+userinfo.loves);
},
error:function(err){
$('#sp2').html("错误的消息:"+err.responseText);
}
});
});
$('#btn').click(function(){
$.ajax({
type:'post',
//发送信息至服务器时内容编码类型。如果采用@ResponseBody,则必须设置contentType
//否则报415错误
contentType :'application/json',
url:'${pageContext.request.contextPath }/testAjax.action',
success:function(list){
var str = '<ul>';
$.each(list,function(i,ui){
str += '<li>'+'行号:'+i+'姓名:'+ui.userName+'</li>';
});
str += '</ul>';
$('#info').html(str);
}
});
});
})
</script>
</head>
<body>
<input type="text" id="username" /><span id="sp"></span>
<input type="text" id="username2" /><span id="sp2"></span>
<input type="button" id="btn" value="ajax请求集合" />
<div id="info"></div>
</body>
</html>
<%
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 'ajax.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="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function(){
$('#username').blur(function(){
$.ajax({
type:'post',
dataType:'json',//服务端返回到客户端的数据格式, text/html
//对应checkName.action中request.getParameter("username")
data:{'username':$('#username').val()},
url:'${pageContext.request.contextPath }/checkName.action',
success:function(map){
$('#sp').html(map.info);
},
error:function(err){
$('#sp').html(err.responseText);
}
});
});
$('#username2').blur(function(){
var jsonObj = JSON.stringify({'userName':$('#username2').val()});
$.ajax({
type:'post',
dataType:'json',
contentType :'application/json', //发送信息至服务器时内容编码类型。
data:jsonObj,
url:'${pageContext.request.contextPath }/checkNameJson.action',
success:function(userinfo){
$('#sp2').html("正确的消息:"+userinfo.loves);
},
error:function(err){
$('#sp2').html("错误的消息:"+err.responseText);
}
});
});
$('#btn').click(function(){
$.ajax({
type:'post',
//发送信息至服务器时内容编码类型。如果采用@ResponseBody,则必须设置contentType
//否则报415错误
contentType :'application/json',
url:'${pageContext.request.contextPath }/testAjax.action',
success:function(list){
var str = '<ul>';
$.each(list,function(i,ui){
str += '<li>'+'行号:'+i+'姓名:'+ui.userName+'</li>';
});
str += '</ul>';
$('#info').html(str);
}
});
});
})
</script>
</head>
<body>
<input type="text" id="username" /><span id="sp"></span>
<input type="text" id="username2" /><span id="sp2"></span>
<input type="button" id="btn" value="ajax请求集合" />
<div id="info"></div>
</body>
</html>