重要通知:域名变更为m.bxuu.net请收藏
/script>
3. 页面展示
1
<div id="divCustors"><\/div>
4. 最终Page Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE ht PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Strict\/\/EN" "http:\/\/w3.org\/TR\/xht1\/DTD\/xht1-strict.dtd">
<ht xns="http:\/\/w3.org\/1999\/xht">
<head>
<title>Top Custors with Callback<\/title>
<\/head>
<body>
<div id="divCustors">
<\/div>
<script type="text\/javascript">
function onCustorLoaded(result, thodNa) {
var ht = '<ul>';
for (var i = 0; i < result.length; i++) {
ht += '<li>' + result[i] + '<\/li>';
}
ht += '<\/ul>';
docunt.getElentById('divCustors').innerHTML = ht;
}
<\/script>
<script type="text\/javascript" src="http:\/\/yiwuku\/Service.aspx?jsonp=onCustorLoaded"><\/script>
<\/body>
<\/ht>
体现
编辑
JQuery下:
1.
$.getJSON
1
2
3
4
5
6
7
8
9
10
11
<script>
$(docunt).ready(function(){
$.getJSON("http:\/\/api.flickr\/services\/feeds\/photos_public.gne?tags=cat&a;tagde=any&a;fort=json&a;jsoncallback=?",
function(data){
$.each(data.ite,
function(i,ite{
$("<i\/>").attr("src",itedia..appendTo("#iges");
if(i==3)returnfalse;
});
});
});
jsoncallback=?,其中 '? '会自动替换为function(data)函数。
2. $.ajax
1
2
3
4
5
6
7
8
9
$.ajax({
dataType:'jsonp',
data:'id=10',
jsonp:'jsonp_callback',
url:'http:\/\/yiwuku\/getdata',
success:function(){
\/\/dostuff
},
});
如何在服务器端实现对JSONP支持
这仅仅需要把服务的JSON数据转换成想要的script tags的形式就可以了,格式可以自已定义,毕竟这是个非官方的协议。
可参考:Ilent JSONP in your Asp Application
注:Callback仅仅是JSONP的简单实现,可以根据具体需要实现更复杂的功能,比如可以在客户端动态集成更多的变量数据来完成分页功能。
Java:
return request.getParater("jsonpcallback")+"("+JsonString+")";
返回内容像:jsonp1382016430883([{"id":1,"title":"XXXX"},{"id":2,"title":"YYYYY"}])