前端:
$.ajax({
type:"get",
async: false,//是否异步
url:"http://ip:端口/servlet地址",
dataType: "jsonp",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
jsonpCallback: "callback",
data: { }, //传递的参数
success: function(e){
var r = $.parseJSON(e);//e:返回值
},
error: function(XMLHttpRequest, textStatus, errorThrown) {}
});
后台:用servlet做的样例
String callback = request.getParameter("callback");//回调函数
request.setCharacterEncoding("utf-8");//防止中文乱码
response.setCharacterEncoding("utf-8");//防止中文乱码
//response.setHeader("pragma", "no-cache");
//response.setHeader("cache-control", "no-cache");
//list要返回的结果
JSONArray jsonArray = JSONArray.fromObject(list);
response.getWriter().print(callback+"('"+jsonArray+"')");
response.getWriter().flush();