例子一 请求别人主机上提供的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);
});
})
服务器端:item_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%>)
原文链接:https://www.f2er.com/json/290676.html