Struts2的jsonp介面執行個體

來源:互聯網
上載者:User

標籤:des   style   http   io   ar   color   os   sp   java   

和以往寫struts2程式一樣,action方法跳轉到一個JSP中,為了配合jsonp的跨域,要在JSP中做一個輸出JSP:<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><% String jsoncallback = request.getParameter("callback");String jsonResult = (String)request.getAttribute("jsonStr");out.println("jsonpcallback("+jsonResult+")");%>其中jsonStr是從action層賦值號的,要傳給介面的傳回值------------------------------------------------------------------------------------------------------------- HTML:注意標紅的地方!<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<meta name="Generator" content="EditPlus®"> 
<meta name="Author" content=""> 
<meta name="Keywords" content=""> 
<meta name="Description" content=""> 
<title>Document</title> 
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script> 
</head> 
<body> 
<script language="javascript"> 
$.ajax({ 
type:"get", 
dataType:"jsonp", 
async:false, 
url: "http://localhost:8080/myCwjm/searchJiemus.action", 
jsonp:"jsonpcallback", 
success: function(response){ 

}}); 

function jsonpcallback(response){ 
alert(response.cate1[0].jmLink); 
} 
</script> 
</body> 
</html>  也可以在地址欄裡直接輸入:http://localhost:8080/myCwjm/searchJiemus.action?callback=jsonpCallback會得到:jsonpcallback({"cate1":[{"jmCate":1,"jmId":1,"jmLink":"http://tv.brtn.cn /20141207/VIDE1417965266460208.shtml","jmName":"和闐玉"}, {"jmCate":1,"jmId":2,"jmLink":"http://finance.brtn.cn/20141204 /VIDE1417670950360636.shtml","jmName":"紅寶石"}],"cate2": [{"jmCate":2,"jmId":3,"jmLink":"http://finance.brtn.cn/20141128 /VIDE1417156870992643.shtml","jmName":"瑪瑙"}, {"jmCate":2,"jmId":4,"jmLink":"http://finance.brtn.cn/20141120 /VIDE1416466960642254.shtml","jmName":"翡翠"}],"cate3":[],"cate4": [],"cate5":[],"cate6":[],"cate7":[],"cate8":[],"cate9":[],"cate10":[],"cate11":[],"cate12":[]}) 這樣一個json串  

Struts2的jsonp介面執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.