加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

jsonp跨域访问(Get方式)

发布时间:2020-12-16 18:56:54 所属栏目:百科 来源:网络整理
导读:前端代码: $.ajax({ type: "GET", url: "http://127.0.0.1:8081/webp/servlet/PersonListServlet?pname="+encodeURI(encodeURI(pname))+"email="+email, dataType: "jsonp", jsonpCallback: "OnGetMemberSuccessByjsonp"//回调处理函数 }); function OnGetM

前端代码:

$.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;//部门

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读