jsonp跨域访问(Get方式)

前端之家收集整理的这篇文章主要介绍了jsonp跨域访问(Get方式)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前端代码

$.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

猜你在找的Json相关文章