ajax小收获

$.ajax({
type: "post",
dataType:"text",
url: "sugController_toAjax",
contentType: "application/x-www-form-urlencoded; charset=utf-8",
data: "index="+i+"&sug_id=${sug.sug_id}&sug_name=${sug.sug_name}&sug_phone=${sug.sug_phone}"+
"&sug_email=${sug.sug_email}&sug_content=${sug.sug_content}&depart_id=${sug.depart.depart_id}",
success: function(data){
alert(data);
if(data.length!=4){
//data接过来的是后台的集合
$("#tab").append(data);
}
}
});

ajax由于并没有刷新页面 因此不能自动将request域里的信息携带过去 如果需要在前台获取request的新数据 需要在后台手动给request设置值

public String toAjax() {
HttpServletRequest request = ServletActionContext.getRequest();
int object =Integer.parseInt(request.getParameter("index"));
page.setPageIndex(object);
Map<String, Object> condMap = new HashMap<>();
// 往map对象里传递参数
condMap.put("sug_id", request.getParameter("sug_id"));
condMap.put("sug_name", request.getParameter("sug_name"));
condMap.put("sug_phone", request.getParameter("sug_phone"));
condMap.put("sug_email", request.getParameter("sug_email"));
condMap.put("sug_content", request.getParameter("sug_content"));
condMap.put("depart_id", request.getParameter("depart_id"));
allSug = sugService.getAllSug(condMap, page);
// 调用本类的方法 返回建议数据
String html = "";
if(allSug.size()!=0){
for (int i = 0; i < allSug.size(); i++) {
html += "<tr><td>" + allSug.get(i).getSug_name() + "</td><td>"
+ allSug.get(i).getSug_phone() + "</td><td>"
+ allSug.get(i).getSug_email() + "</td><td>"
+ allSug.get(i).getSug_content() + "</td><td>"
+ allSug.get(i).getDepart().getDepart_name() + "</td></tr>";
}
}
else{
html="没有数据";
}

HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(html);
return null;
}

我会因为您的一个点赞而沾沾自喜,但我更会因为您的一次批评而急不可耐,希望你我共同进步
原文地址:https://www.cnblogs.com/lyaml/p/7660449.html