java json-lib & jQuery & jsonp

来源:互联网 发布:深圳龙岗ug编程培训 编辑:程序博客网 时间:2024/05/18 01:31
参考链接: 
1、http://hanqunfeng.iteye.com/blog/1866712 
2、http://blog.csdn.net/z69183787/article/details/15808921 
3、http://www.cnblogs.com/JerryTian/p/4194900.html 
4、http://feitianbenyue.iteye.com/blog/2046877 
一 程序所需jar文件及POM 
Java代码  收藏代码
  1. <dependency>  
  2.             <groupId>net.sf.json-lib</groupId>  
  3.             <artifactId>json-lib</artifactId>  
  4.             <version>2.4</version>  
  5.         </dependency>  
  6.           
  7.         <dependency>  
  8.             <groupId>net.sf.ezmorph</groupId>  
  9.             <artifactId>ezmorph</artifactId>  
  10.             <version>1.0.6</version>  
  11.         </dependency>  


二 后台代码编写 
Java代码  收藏代码
  1. try {    
  2.         response.setContentType("text/plain");    
  3.         response.setHeader("Pragma""No-cache");    
  4.         response.setHeader("Cache-Control""no-cache");    
  5.         response.setDateHeader("Expires"0);    
  6.         Map<String,String> map = new HashMap<String,String>();     
  7.         map.put("result""content");    
  8.         PrintWriter out = response.getWriter();         
  9.         JSONObject resultJSON = JSONObject.fromObject(map); //根据需要拼装json    
  10.         String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数    
  11.         out.println(jsonpCallback+"("+resultJSON.toString(1,1)+")");//返回jsonp格式数据    
  12.         out.flush();    
  13.         out.close();    
  14.       } catch (IOException e) {    
  15.        e.printStackTrace();    
  16.       }    

三 JS代码编写 
Java代码  收藏代码
  1. $.ajax({    
  2.         type : "get",    
  3.         async:false,    
  4.         url : "http://app.example.com/base/json.do?sid=1494&busiId=101",    
  5.         dataType : "jsonp",//数据类型为jsonp    
  6.         jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数    
  7.         success : function(data){    
  8.             $("#showcontent").text("Result:"+data.result)    
  9.         },    
  10.         error:function(){    
  11.             alert('fail');    
  12.         }    
  13.     });     

四 相关错误 
Java代码  收藏代码
  1. java.lang.ClassCastException: JSON keys must be strings  

解决如下: 
Java代码  收藏代码
  1. JsonConfig jsonConfig = new JsonConfig();    
  2.    
  3.    // 排除,避免循环引用 There is a cycle in the hierarchy!    
  4.    jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);    
  5.    jsonConfig.setIgnoreDefaultExcludes(true);    
  6.    jsonConfig.setAllowNonStringKeys(true);    
  7.    
  8.    if (Validator.isNotNullOrEmpty(excludes)){    
  9.        jsonConfig.setExcludes(excludes);    
  10.    }    
  11.    String string = JsonUtil.toJSON(obj, jsonConfig).toString(44);    

五 相关说明 
jsonp 
类型:String 
在一个 jsonp 请求中重写回调函数的名字。这个值用来替代在 "callback=?" 这种 GET 或 POST 请求中 URL 参数里的 "callback" 部分,比如 {jsonp:'onJsonPLoad'} 会导致将 "onJsonPLoad=?" 传给服务器。 
jsonpCallback 
类型:String 
为 jsonp 请求指定一个回调函数名。这个值将用来取代 jQuery 自动生成的随机函数名。这主要用来让 jQuery 生成度独特的函数名,这样管理请求更容易,也能方便地提供回调函数和错误处理。你也可以在想让浏览器缓存 GET 请求的时候,指定这个回调函数名。 

0 0
原创粉丝点击