Front end:
$.ajax({
type: "get",
async: false,//Whether it is asynchronous
url: "http://ip:port/servlet address",
dataType: "jsonp",
contentType : "application/x-www-form-urlencoded;charset=UTF-8",
jsonpCallback: "callback",
data: { }, //Parameters passed
success: function(e){
var r = $ .parseJSON(e);//e: return value
},
error: function(XMLHttpRequest, textStatus, errorThrown) {}
});
Backend: example using servlet
String callback = request.getParameter(" callback");//Callback function
request.setCharacterEncoding("utf-8");//Prevent Chinese garbled characters
response.setCharacterEncoding("utf-8");//Prevent Chinese garbled characters
//response.setHeader(" pragma", "no-cache");
//response.setHeader("cache-control", "no-cache");
//list the results to be returned
JSONArray jsonArray = JSONArray.fromObject(list);
response.getWriter().print(callback+"('"+jsonArray+"')");
response.getWriter().flush();