首先 我要说 very 简单
两个事儿
1、jsp界面显示代码 :
jQuery.ajax({
url:"<%=request.getContextPath()%>/servlet/data_jd", //将要跳转的 servlet
type:"post", //传值方式
data:"type="+selectedcity_id, //要传的值
dataType:"json", //返回格式
success:function(json){ // 返回的结果显示
var cc = json.array; //这是我返回的结果
cc = "["+cc+"]"; //对结果进行 处理
//以上就是 Ajax 异步 前台处理 代码
2、第二部 是servlet 中的写法, 记住要写在 dopost方法中 首先把该定义的东西都定义好
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");//这里是转码
request.setCharacterEncoding("utf-8");//这里是转码 (如果还是乱码,请检查此项目的编码格式,要改成UTF-8)
//以上是发送到客户端的相应类型
PrintWriter out = response.getWriter();//定义一个 out(用于写回去东西)
JSONArray array = new JSONArray(); //定义 集合
JSONObject filejson = new JSONObject(); //定义容器
filejson.put("array", array);//将数组存入容器
out.print(filejson);//返回到jsp页面
out.flush();//清空缓存
out.close();//关闭 out
这样一来 在前台的 success 中 可以找到 返回的内容 , 你可以尝试 打印下 json.array 就是你传过来的数组
很简单吧