1、使用ajax方式
$ajax({
type: xxx,
url: xxx,
async:true,
data:xxx,
dataType:"json",
success:function(xxx){
xxxxxx
}
})
2、使用post方式
$.post(url,data,function,dataType)
3、使用get方式
$.post(url,function,dataType)
4、做一个省份,城市,区县下拉列表
4.1、写servlet
4.1.1、ProServlet
public class ProServlet extends BaseServlet {
public void pro(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ProDao proDao = new ProDaoImpl();
List<Pro> list = proDao.searchAll();
String result = JSON.toJSON(list).toString();
resp.setContentType("text/plain;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.write(result);
}
}
4.1.2、CityServlet
public class CityServlet extends BaseServlet {
public void city(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pid = req.getParameter("pid");
CityDao cityDao = new CityDaoImpl();
List<City> list = cityDao.searchByPid(Integer.parseInt(pid));
String result = JSON.toJSON(list).toString();
resp.setContentType("text/plain;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.write(result);
}
}
4.1.3、AreaServlet
public class AreaServlet extends BaseServlet {
public void area(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String cid = req.getParameter("cid");
AreaDao areaDao = new AreaDaoImpl();
List<Area> list = areaDao.searchByCid(Integer.parseInt(cid));
String result = JSON.toJSON(list).toString();
resp.setContentType("text/plain;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.write(result);
}
}
4.2 配置Servlet
<servlet>
<servlet-name>proServlet</servlet-name>
<servlet-class>com.yang.servlet.ProServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>proServlet</servlet-name>
<url-pattern>/pro</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cityServlet</servlet-name>
<servlet-class>com.yang.servlet.CityServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cityServlet</servlet-name>
<url-pattern>/city</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>areaServlet</servlet-name>
<servlet-class>com.yang.servlet.AreaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>areaServlet</servlet-name>
<url-pattern>/area</url-pattern>
</servlet-mapping>
4.3 前端页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
function loadPro(){
var pro = $("#pid");
$.ajax({
type:"get",
url:"pro",
async:true,
data:"op=pro",
dataType:"json",
success:function(result){
$.each(result, function(index, item){
pro.append("<option value='"+item.id+"'>"+item.pname+"</option>")
})
}
})
}
function loadCity(){
var city = $("#cid");
var pid = $("#pid").val();
$.post("city", "op=city&pid="+pid,
function(result){
city.html("<option value='0'> --请选择--</option>");
$.each(result, function(index, item){
city.append("<option value='"+item.id+"'>"+item.cname+"</option>");
})
},"json")
}
function loadArea(){
var area = $("#aid");
var cid = $("#cid").val();
$.get("area?op=area&cid="+cid,
function(result){
area.html("<option value='0'> --请选择--</option>");
$.each(result, function(index, item){
area.append("<option value='"+item.id+"'>"+item.aname+"</option>");
})
},"json")
}
</script>
</head>
<body onload="loadPro()">
<div>
省份
<select name="pid" id="pid" onchange="loadCity()">
<option value="0"> --请选择--</option>
</select>
城市
<select name="cid" id="cid" onchange="loadArea()">
<option value="0"> --请选择--</option>
</select>
区县
<select name="aid" id="aid">
<option value="0"> --请选择--</option>
</select>
</div>
</body>
</html>
4.4 页面效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/919f5053ad3b0f88ca0bcac4d7a1982a.png#pic_center)