jsonp 常用java方法
(1)以jsonp的形式返回:函数名(json字符串)
/*** * 用于jsonp调用 * @param map : 用于构造json数据 * @param callback : 回调的javascript方法名 * @param filters : <code>SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter .serializeAllExcept("content"); FilterProvider filters = new SimpleFilterProvider().addFilter( Constant2.SIMPLEFILTER_JACKSON_PAPERNEWS, theFilter);</code> * @return : js函数名(json字符串) */ public static String getJsonP(Object map,String callback,FilterProvider filters) { if(ValueWidget.isNullOrEmpty(map)){ return null; } String content = null; if(map instanceof String){ content=(String)map; }else{ ObjectMapper mapper = getObjectMapper(); ObjectWriter writer=null; try { if(filters!=null){ writer=mapper.writer(filters); }else{ writer=mapper.writer(); } content=writer.writeValueAsString(map); logger.info(content); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } if(ValueWidget.isNullOrEmpty(callback)){ return content; } return callback+"("+content+");"; }
应用:
@ResponseBody @RequestMapping(value = "/json_detail", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF) public String jsonDetail(Model model, int id/*新闻的id*/, PaperNewsView view, HttpSession session, HttpServletRequest request, String callback) throws IOException { init(request); PaperNews news=(PaperNews) getDao().get(id); String content; /*int comment_type=0; if(type==2){//新闻 comment_type=Constant2.COMMENT_TARGET_TYPE_NEWS; }else if(type==1){ }*/ String title=null; if(news.getType()==Constant2.TYPE_NEWS){ title="新闻"; }else{ title="报料"; } long commentCount=this.newsCommentDao.getCount(null, id, 1); news.setCommentSum(commentCount); if(!ValueWidget.isNullOrEmpty(news.getPic())){ news.setPic(JSONPUtil.getPicPath(news.getPic())); } content = getJsonP(news, callback); AccessLog accessLog=logInto(request); accessLog.setDescription("手机端"+title+"详情,id:"+id); logSave(accessLog, request); return content; }
(2)去掉callback
callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"}
/*** * callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"} * @param input * @param callbackName * @return */ public static String deleteCallback(String input,String callbackName){ return input.replaceAll("^"+callbackName+"\\((.*\\})\\);?$", "$1"); }
应用:
/*** * convert json string to Map;e.g:{errorMessage=系统异常,请稍后再试!, id=, status=02, errorCode=error_default, method=} * @param jsonResult * @return * @throws UnsupportedEncodingException * @throws JSONException * @throws org.json.JSONException */ public static Map<String, String> getMap(String jsonResult) throws UnsupportedEncodingException, JSONException, org.json.JSONException { if(ValueWidget.isNullOrEmpty(jsonResult)){ return null; } //callback({"auth_code":"v39hXq"}) jsonResult=deleteCallback(jsonResult, "callback"); Map<String, String> resultMap =null; Map obj = (Map) JsonParser.parserRandomJsonFormat(jsonResult); if (ValueWidget.isNullOrEmpty(obj)) { return null; } List resultList = (List) obj.get("resultList"); if(ValueWidget.isNullOrEmpty(resultList)){ resultMap=obj; }else{ resultMap= new HashMap<String, String>(); for (int i = 0; i < resultList.size(); i++) { Map mp_tmp = (Map) resultList.get(i); parseMap(resultMap, mp_tmp); } } return resultMap; }
参考:http://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html
相关推荐
详解JSON和JSONP劫持以及解决方法.docx
调用方法: $.ajax({ type:'get', async:true, url:'地址', dataType:'jsonp', jsonp:'callback', jsonCallback:'自定义的一个名字', success:function(data){ console.log(data); } });
本文主要介绍的是在VUE2.0Jsonp的使用方法,通过github引入jsonp和promise封装,实现跨域获取数据。 1.JSONP的用途和原理 使用JSONP主要是目的通过动态创建Script,动态拼接url,进而抓取数据,实现跨域。确切地说,...
使用jsonp跨域获取json数据。Ajax获取JAVA服务器json数据。
这篇文章是关于如何在自己的网站中实现百度搜索框的效果,所利用的是jsonp相关的知识点,所以在代码实现之前先普及一下jsonp是用来干嘛的。
jsonp的原理 jsonp的使用,使用jsonp解决js跨域问题!
JSONP 实例
Jsonp和java操作
交接jsonp,轻松实现跨域方法的调用,很值得学习的东西
后台php设置jsonp
解决跨域封装的jsonp
本文主要介绍的是在VUE2.0Jsonp的使用方法,通过github引入jsonp和promise封装,实现跨域获取数据。
本篇文章主要介绍了用nodejs实现json和jsonp服务的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
总算理解jsonp的应用了,以前只知道jsonp但是还是不会再代码中应用,看了这个以后突然恍然大悟,妈妈再也不用担心我学不会jsonp了(/坏笑,以上纯属虚构)
在页面a中由前端发送一个jsonp请求到客户方,得到一个token值 前端得到token值后向自己后端发送一个请求,后端根据token去redis(token的值就是redis里的key)里取值(key=token的值,value=用户信息等)判断用户是否已...
使用Jquery中的Aajx方法利用Jsonp方式访问豆瓣公开的api接口,跨域获取书籍信息。
前端使用jquery,datatype采用jsonp,服务端采用C#编写的webService
纯servlet实现JSONP 非常简单易懂的一个例子。
jsonp XML解析