天天看點

jsonp跨域請求的小例子

渚嬪瓙涓€ 璇鋒眰鍒漢涓繪満涓婃彁渚涚殑JSONP鏈嶅姟(杩滅▼JSON鏈嶅姟锛夛細

<html>

<script聽 type="text/javascript" src="../js/jquery/jQuery.js"></script>

<script type="text/javascript"/>

function crossDomain(){

聽聽 var url = "http://services.digg.com/stories/top?appkey=http%3A%2F%2Fmashup.com&type=javascript&callback=?";

聽聽聽聽聽聽 jQuery.getJSON(url, function(data){

聽聽聽聽聽聽聽聽聽 $("#result").html(data.count);

聽聽聽聽聽聽聽 });

}

</script>

<body >

<div id="result"></div>

<div聽 align="center"><input聽 type="button" name="print"聽 onClick="crossDomain();" value="璺ㄥ煙璇鋒眰"></div>

</body>

</html>

鍙傝€冭祫鏂欙細

http://www.oschina.net/question/12_7702

http://www.iteye.com/topic/169765

http://wenku.baidu.com/view/e97c8eb465ce05087632130d.html

渚嬪瓙浜岋細json璺ㄥ煙璇鋒眰锛屽寘鎷鎴風鍜屾湇鍔″櫒绔?

瀹㈡埛绔細

$(document).ready(function(){

var聽 url="http://192.168.10.33:8415/item_count.jsp?callback=";

聽聽 聽jQuery.getJSON(url, function(data){

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 var itemNumber=聽data[0].count;聽

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 $("#itemNumber").html(itemNumber);

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 });

})

鏈嶅姟鍣ㄧ锛歩tem_count.jsp

<%@ page import="java.util"聽聽

聽聽language="java"

聽聽contentType="text/html;charset=GBK"

%><%

聽request.setCharacterEncoding("GBK");

聽聽聽 String callback=request.getParameter("callback");

聽聽聽 int聽聽 itemNumber=10;

String countStr="[{count:"+itemNumber+"},{count:"+feeNumber+"},{count:"+annualNumber+"},{count:"+agentNumber+"},{count:"+entNumber+"}]";

%><%=callback%>(<%=countStr%>)