前端代码:
$.ajax({
type: "GET",
url: "http://127.0.0.1:8081/webp/servlet/PersonListServlet?pname="+encodeURI(encodeURI(pname))+"&email="+email,
dataType: "jsonp",
jsonpCallback: "OnGetMemberSuccessByjsonp"//回调处理函数
});
function OnGetMemberSuccessByjsonp(data) {
//处理data
var arr = data.personlist;
for(i = 0; i < arr.length; i++) {
var person = arr[i];
alert(person.pid+"--"+person.department.deptmentName+"--"+person.email);//注意pid department必须和Person类的属性完全一致,包括大小写
}
}
后端代码:
Servlet类:
List<Person> list = PersonDao.getPersonByCon(condition);
String json = JSONArray.fromObject(list).toString();
//System.out.println(json);
String result = "{\"personlist\":"+json+"}";
String callback = req.getParameter("callback");
String jsoncallback =( callback + "("+result+")").replaceAll("\"","\'");
//System.out.println(jsoncallback);
PrintWriter out = resp.getWriter();
out.print(jsoncallback);
out.flush();
out.close();
Person类:
public class Person {
private int Pid;//id
private String email;//邮箱
private String telephone;//手机
private Department department;//部门
}
原文链接:/json/289314.html