1.ajax端
$.ajax({ type: "post", dataType: "jsonp", //传递给请求处理程序,用以获得jsonp回调函数名的参数名(默认为:callback) jsonp: "callback", //自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 jsonpCallback:"success_jsonpcallback", url: "http://localhost:8080/vdt/json/queryMetadata.json", success: function (msg) { alert("succes"); }, error:function(e){ alert("error"); } });
2.java端
@RequestMapping("/queryMetadata") @ResponseBody public Object queryMetadata(String id,String callback) { MetaDataBeanVo metaDataBeanVo= mampManager.getMetaDataSource("f4050e690a62402a8c87668eb10ed425,f4050e690a62402a8c87668eb10ed425"); List<MetaDataBeanVo> list=new ArrayList<MetaDataBeanVo>(); list.add(metaDataBeanVo); GenericResult gr = new GenericResult(); gr.setResult(list); gr.setMessage("查询元数据"); gr.setSuccess(true); gr.setErrorCode(""); //兼容jsonp 和json模式 if(StringUtils.isEmpty(callback)){ return gr; }else{ MappingJacksonValue mappingJacksonValue=new MappingJacksonValue(gr); mappingJacksonValue.setJsonpFunction(callback); return mappingJacksonValue; } }