- 定义处理结果的js函数:
function m(p1){
console.log(p1)
}
- ajax请求,同时指定拿到结果后的回调函数
$.ajax({
url:"xxx",
type:"GET",
dataType: 'jsonp',
jsonpCallback:"m", // jsonp指定回调函数参数名,jsonpCallback指定回调函数
contentType: "application/jsonp; charset=utf-8",
success:function (param0,param1,param2){
console.log(param0,param1,param2)
},
error:function (param0,param1,param2){
console.log(param0,param1,param2)
},
complete:function (param0,param1,param2){
console.log(param0,param1,param2)
}
});
- 服务端进行处理,当有callback参数时,返回对应的格式String。(方法名+“(”+‘服务端结果String’ +“)”)
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/xxx",produces = "text/plain;charset=UTF-8")
public String getSrMsg(HttpServletRequest request, HttpServletResponse servletResponse){
String callBack = request.getParameter("callback");
if(callBack != null){
return callBack + "(" + JSON.toJSONString(response) + ")";
}
return JSON.toJSONString(response);
}
- 解决返回乱码
@RequestMapping(method = RequestMethod.GET, value = "/xxx",produces = "text/plain;charset=UTF-8")