ajax的基础语法使用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<script type="text/javascript" src="js/jquery-1.9.0.js"></script>
<script type="text/javascript">
$(function() {
$("button").click(function() {
var userName = $("#exampleInputEmail1").val();
var pwd = $("#exampleInputPassword1").val();
$.ajax({
type : "post",
url : "/ajaxProject/IsLoginServlet",
data : {"userName":userName,"pwd":pwd},
dataType : "json",
success : function(data) {
if(data=="1"){
window.location.href="show.jsp";
}else{
/* 提示用户登录信息错误 */
$("#warm").html("用户名或密码错误!");
}
}
});
});
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="form-group col-md-6">
<label for="exampleInputEmail1">用户名</label> <input type="text"
class="form-control" id="exampleInputEmail1" placeholder="UserName"
name="userName" /><span id="warm"></span>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for="exampleInputPassword1">密码</label> <input type="password"
class="form-control" id="exampleInputPassword1"
placeholder="Password" name="pwd" />
</div>
</div>
<button type="submit" class="btn btn-default" id="btn">登录</button>
</div>
</body>
</html>