前端:
$.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();
