码迷,mamicode.com
首页 > Web开发 > 详细

跨域访问:jquery ajax jsonp的实现方法(jsp和action方式)

时间:2014-08-23 20:17:01      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:blog   http   color   java   os   io   strong   for   ar   

 

[javascript]
  1. <mce:script language="javascript"><!--  
  2.     $(function(){      
  3.         $.ajax({  
  4.                    type: "get",  
  5.                    url: "www.sssss.com/ddd.do",  
  6.                    data: {  
  7.                         area : "ddd",  
  8.                         areaid : "2",  
  9.                         categorySz : ["",   
  10.                                     "",   
  11.                                     "",   
  12.                                     "",   
  13.                                     "",   
  14.                                     "208",   
  15.                                     "",   
  16.                                     "",   
  17.                                     "205",   
  18.                                     "205",  
  19.                                     "206"  
  20.                                     ,"207,250,251,252,253"  
  21.                                     ,"207,254"  
  22.                                     ,"207,255"  
  23.                                     ,"207,256"  
  24.                                     ,"207,257"  
  25.                                     ,"207,258"  
  26.                                     ,"207,259"],  
  27.                         typeSz : ["520,1201,201",   
  28.                                 "520,1201,202",   
  29.                                 "520,1202",   
  30.                                 "520,1203",   
  31.                                 "520,1204",   
  32.                                 "",   
  33.                                 "521,1201,201",   
  34.                                 "521,1201,202",   
  35.                                 "1201,202",   
  36.                                 "1202"  
  37.                                 , ""  
  38.                                 , ""  
  39.                                 , ""  
  40.                                 , ""  
  41.                                 , ""  
  42.                                 , ""  
  43.                                 , ""  
  44.                                 , ""],  
  45.                         categoryOrSz : ["",   
  46.                                     "",   
  47.                                     "",   
  48.                                     "",   
  49.                                     "",   
  50.                                     "",   
  51.                                     "",   
  52.                                     "",   
  53.                                     "",   
  54.                                     ""  
  55.                                     , ""  
  56.                                     , "250,251,252,253"  
  57.                                     , ""  
  58.                                     , ""  
  59.                                     , ""  
  60.                                     , ""  
  61.                                     , ""  
  62.                                     , ""]},  
  63.                    dataType : "jsonp",  
  64.                    jsonp: "callback",  
  65.                    async: false,  
  66.                    cache: false,  
  67.                    success: function(data){  
  68.                     if (data != null && data != "") {  
  69.                                                                                                                               
  70.                     }                         
  71.                    }  
  72.                 });  
  73.     });  
  74. // --></mce:script>  

 

 

服务器端:

代码很简单,就是输出一个字符串
比如正常输出json应该是:[{"id":"1","name":"测试1"},{"id":"2","name":"测试2"}]
jsonp 则输出: jsonpcallback([{"id":"1","name":"测试1"},{"id":"2","name":"测试2"}]) 其中“jsonpcallback”是客户端传过来的

 

  1. /** 
  2.  *  
  3.  * @param form 
  4.  * @param request 
  5.  * @param response 
  6.  * @return 
  7.  * @throws Exception 
  8.  */  
  9. public String ajaxGetMybusAllDataJsonp(ActionForm form,  
  10.         HttpServletRequest request, HttpServletResponse response)  
  11.         throws Exception {  
  12.     String callback = request.getParameter("callback");  
  13.     JSONObject res = new JSONObject();  
  14.       
  15.     /* 
  16.      * 处理方法 
  17.      *  
  18.      */  
  19.     res.put("d", "dddd");  
  20.     // 可以避免前台显示出现乱码  
  21.     response.setContentType("text/html");  
  22.     response.setCharacterEncoding("utf-8");  
  23.     PrintWriter out = response.getWriter();  
  24.     out.print(callback + "(" + res.toString() + ")");  
  25.     out.flush();  
  26.     out.close();  
  27.     return null;  
  28. }  

 

跨域访问:jquery ajax jsonp的实现方法(jsp和action方式)

标签:blog   http   color   java   os   io   strong   for   ar   

原文地址:http://www.cnblogs.com/htys/p/3931634.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!