JavaScript实现异步调用例子
======================
js部分:
var url = "<%=request.getContextPath()%>/servlet/AjaxCheckCallListServlet?assignCallIdList="+assignCallIdStr;
var checkResult = "";
$.ajax({
async : false, //false表示同步,true表示异步
type : "post",
url : url,
success : function(result) {
checkResult = result; //result是返回数据
}
});
if (checkResult != "") {
alert(checkResult + " not belong to the current UM. ");
return false;
}
servlet部分:
public class AjaxCheckCallListServlet extends DBServlet {
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private HttpServletResponse response;
protected String perform(HttpServletRequest request,
HttpServletResponse response, StrutsMessage message)
throws ServletException, IOException, SQLException, DataException,
Exception {
this.response = response;
this.request = request;
User user = (User)request.getSession().getAttribute("user");
String userId = user.getUser_id();
String assignCallIdList = Utilities.replaceNullWithBlank(request.getParameter("assignCallIdList"));
CallListDao callListDao = new CallListDao();
String ret = callListDao.checkCallList(assignCallIdList, userId);
if(!ret.isEmpty()){
ret = ret.substring(0, ret.length()-1);
}
response.getWriter().print(ret); //将结果写到response里
return null;
}
}