zoukankan      html  css  js  c++  java
  • 跨域访问:jquery ajax jsonp的实现方法(jsp和action方式)

    [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. }  
  • 相关阅读:
    四,awk格式化
    printf命令详解
    三,awk变量
    二,awk分隔符
    一,AWK基础
    【leetcode_easy_array】1399. Count Largest Group
    【leetcode_easy_array】1184. Distance Between Bus Stops
    【leetcode_easy_array】1346. Check If N and Its Double Exist
    【leetcode_easy_array】1304. Find N Unique Integers Sum up to Zero
    【leetcode_easy_array】1337. The K Weakest Rows in a Matrix
  • 原文地址:https://www.cnblogs.com/htys/p/3931634.html
Copyright © 2011-2022 走看看