<%@ 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 '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="<%=basePath + "/js/jquery-1.8.0.js"%>"></script>
<script type="text/javascript">
function checkLogin()
{
var username = $("#username").val();
var pwd = $(".pwd").val();
if(username == "" || pwd == "")
{
alert("用户名和密码不能为空!");
return false;
}
return true;
}
function ajax()
{
//todo :发送异步请求,查询账户名是否存在
var url = "<%=basePath + "login/doQuery.jsp"%>"; //请求地址
//表单参数格式,{p1:"v1", p2:"v2"};
//$.post(url, params, function(data){});
$.post(url, {name:$("#username").val()},
function(data)
{
if(data==1)
{
$("#msg").html("<font color='red'>已存在</font>");
}
else
{
$("#msg").html("可以注册");
}
}
);
}
</script>
</head>
<body>
<form action="<%=basePath + "/doLoginServlet"%>" method="post" οnsubmit="return checkLogin()">
<table border="1">
<tr>
<td>用户名: </td>
<td><input type="text" name="username" id="username" value="" οnblur="ajax()"/>
<span id="msg"></span>
</td>
</tr>
<tr>
<td>密码: </td>
<td><input type="password" name="pwd" class="pwd"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录"/>
</td>
</tr>
</table>
</form>
</body>
</html>
java-ajax使用方法(异部请求)
最新推荐文章于 2023-05-23 15:22:15 发布