$.ajax({
url:" ",
type:" ", //post,get传输方式
data:" ",
async:false, //默认true,异步.
success:function(mes)
{
}
})
例子
$.ajax({
url:"<%=path%>/checkRegisterNameRepetitionServlet",//servlet路径
type:"post",
data:"userName="+userName,
async:false,//是否异步,默认为异步,false为同步状态,线程排队进行
success:function(mes){
if(mes=="1")//重复
{
$("#userName").next().html("x 用户名重复");
$("#userName").next().css("color","red");
f = false;
}
if(mes=="0")//不重复
{
$("#userName").next().html("√");
$("#userName").next().css("color","green");
f = true;
}
}
})
ajax简写
$.ajax({
url:"",
type:"",
data:"{id:'45',name:'sanmao'}",
success:function(mes)
{
}
})
$.post(url,data,
function(mes)
{
}
);
$.post("{pageContext.request.contextPath}/front/register.jsp",{id:'45',name:'sanmao'}
function(mes)
{
if(mes=="1")
{
$("#captcha").next().html("√");
$("#captcha").next().css("color","green");
flag = true;
}
}
)
ajax加载另一个JSP页面
另一个JSP页面,这里举例表格+分页
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
%>
<table class="result-tab" width="100%">
<tr>
<th class="tc" width="5%"><input class="allChoose" name="" type="checkbox"></th>
<th>序号</th>
<th>ID</th>
<th>标题</th>
<th>图片</th>
<th>类型</th>
<th>用户</th>
<th>更新时间</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.questions}" var="question" varStatus="stat">
<tr>
<td class="tc"><input name="id[]" value="59" type="checkbox"></td>
<td>${stat.count}</td>
<td>${question.id}</td>
<td title="${question.title}"><a target="_blank" href="#">${question.title}</a>
</td>
<td><img src="${pageContext.request.contextPath}/uploadImages/${question.pic}"></td>
<td>${question.questionType.title}</td>
<td>${question.userinfo.name}</td>
<td>${question.inputTime}</td>
<td>
<a class="link-update" href="#">修改</a>
<a class="link-del" href="#">删除</a>
</td>
</tr>
</c:forEach>
</table>
<div class="list-page"> ${pager.dataCount} 条
<c:forEach begin="1" end="${requestScope.pager.pageCount}" var="i">
<c:if test="${i eq requestScope.pager.pageNum}" var="isCur">
<span class="current">${i}</span>
</c:if>
<c:if test="${not isCur}">
<a href="#">${i}</a>
</c:if>
${pager.pageNum}/${pager.pageCount} 页</div>
ajax代码
ajax将另一个jsp页面加到本JSP页面中
<script>
function initData(pageNum, questionTypeId, title) {
$.get(path + "/servlet/QuestionServlet?action=findAll",
{pageNum: pageNum, questionTypeId: questionTypeId, title: title},
function (data) {
$(".result-content").html(data); //ajax将另一个jsp页面加到本JSP页面中
});
}
</script>
Servlet代码
List<Question> questions = QuestionDAO.getDao().findAll(pager,whereStr.toString());
request.setAttribute("questions",questions);
request.setAttribute("pager",pager);
this.getServletContext().getRequestDispatcher("/manage/question/listdata.jsp").forward(request,response);