java web get請求跨域

來源:互聯網
上載者:User

標籤:cat   構造   注意   utf-8   string   result   設定   color   except   

http://hw1287789687.iteye.com/blog/2188617

 

java  web中如何跨域請求呢?

使用jsonp,詳情請參考:http://json-p.org/

頁面代碼如下:

Html代碼  
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head lang="en">  
  4.     <meta charset="UTF-8">  
  5.     <title></title>  
  6.     <script type="application/javascript"  >  
  7.         function jsonpCallback(result) {    
  8.             alert(JSON.stringify(result));    
  9.             /*for(var i in result) {    
  10.                 alert(i+":"+result[i]);//迴圈輸出a:1,b:2,etc.    
  11.             }  */  
  12.         }    
  13.         var JSONP=document.createElement("script");    
  14.         JSONP.type="text/javascript";    
  15.         JSONP.src="http://192.168.0.100:8080/tv_mobile/video/text2?callback=jsonpCallback";    
  16.         document.getElementsByTagName("head")[0].appendChild(JSONP);    
  17.      </script>  
  18. </head>  
  19. <body>  
  20.   
  21. </body>  
  22. </html>  

 在瀏覽器中訪問的效果:

 

後台採用spring mvc:

Java代碼  
  1. @ResponseBody  
  2.     @RequestMapping(value = "/text2",produces=SystemHWUtil.RESPONSE_CONTENTTYPE_JAVASCRIPT2 )  
  3.     public String text2(HttpServletRequest request, HttpServletResponse response,String contentType2,String callback)  
  4.             throws IOException {  
  5.         String content = null;  
  6.         Map map = new HashMap();  
  7.   
  8.         map.put("fileName", "a.txt");  
  9.         content=JSONPUtil.getJsonP(map, callback);  
  10.         System.out.println(content);  
  11.         return content;  
  12.   
  13.     }  

 JSONPUtil.getJsonP 靜態方法的實現如下:

Java代碼  
  1. /*** 
  2.      * 用於jsonp調用 
  3.      * @param map : 用於構造json資料 
  4.      * @param callback : 回調的javascript方法名 
  5.      * @return 
  6.      */  
  7.     public static String getJsonP(Map map,String callback)  
  8.     {  
  9.         ObjectMapper mapper = new ObjectMapper();  
  10.         String content = null;  
  11.         try {  
  12.             content = mapper.writeValueAsString(map);  
  13.             System.out.println(content);  
  14.         } catch (JsonGenerationException e) {  
  15.             e.printStackTrace();  
  16.         } catch (JsonMappingException e) {  
  17.             e.printStackTrace();  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         if(ValueWidget.isNullOrEmpty(callback)){  
  22.             return content;  
  23.         }  
  24.         return callback+"("+content+")";  
  25.     }  

 依賴jackson 庫

後台返回的內容是:jsonpCallback({"fileName":"a.txt"})

content type是

 

注意:後台返回的形式是:函數名(參數),此處的函數名就是回呼函數的名稱

 

參考:

spring mvc設定應答體的content type

AJAX 跨域請求 - JSONP擷取JSON資料:http://justcoding.iteye.com/blog/1366102

 

App Framework發送JSONP請求(3):http://hw1287789687.iteye.com/blog/2190719

java web get請求跨域(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.