1) 什么是JSONP?
JSONP是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实现形式)。
2) 如何使用JSONP?
1. 在客户端调用提供JSONP支持的URL Service,获取JSONP格式数据。
比如客户想访问http://www.myserver.com/myService.aspx?jsonp=callbackFunction
假设客户期望返回JSON数据:[“customername1","customername2"]
那么直正返回到客户端的Script Tags: callbackFunction([“customername1","customername2"])
可能的调用方式:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><script type="text/javascript" src="http://www.myserver.com/myService.aspx?jsonp=callbackFunction" />
2. 在客户端写callbackFunction函数的实现
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><script type="text/javascript">
function onCustomerLoaded(result, methodName)
{
var html = '<ul>';
for(var i = 0; i < result.length; i++)
{
html += '<li>' + result[i] + '</li>';
}
html += '</ul>';
document.getElementById('divCustomers').innerHTML = html;
}
</script>
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
3. 页面展示
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><div id="divCustomers"></div>
4. 最终Page Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Top Customers with Callback</title>
</head>
<body>
<div id="divCustomers"></div>
<script type="text/javascript">
function onCustomerLoaded(result, methodName)
{
var html = '<ul>';
for(var i = 0; i < result.length; i++)
{
html += '<li>' + result[i] + '</li>';
}
html += '</ul>';
document.getElementById('divCustomers').innerHTML = html;
}
</script>
<script type="text/javascript" src="http://www.myserver.com/myService.aspx?jsonp=callbackFunction"></script>
</body>
</html>
3) JSONP在JQuery中如何体现的
1. $.getJSON
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><script>
$(document).ready(function(){
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?",
function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images");
if ( i == 3 ) return false;
});
});
});
</script>
jsoncallback=?,其中?会自动替换为function(data)函数。
2. $.ajax
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->$.ajax({
dataType: 'jsonp',
data: 'id=10',
jsonp: 'jsonp_callback',
url: 'http://myotherserver.com/getdata',
success: function () {
// do stuff
},
});
4) 如何在服务器端实现对JSON支持?
这仅仅需要把服务的JSON数据转换成想要的script tags的形式就可以了,格式可以自已定义,毕竟这是个非官方的协议。
可参考:Implement JSONP in your Asp.net Application
注:Callback仅仅是JSONP的简单实现,可以根据具体需要实现更复杂的功能,比如可以在客户端动态集成更多的变量数据来完成分页功能。
分享到:
相关推荐
JSONP简单调用实例。ASP.NET和纯HTML。jQuery的$.ajax的调用!jsonP说白了,就是在json字符串外面包上一个:参数名称+左右括弧!只是包了个:jsonpCallback() 而已! 相关文章:...
解决跨域封装的jsonp
jsonp的原理 jsonp的使用,使用jsonp解决js跨域问题!
在页面a中由前端发送一个jsonp请求到客户方,得到一个token值 前端得到token值后向自己后端发送一个请求,后端根据token去redis(token的值就是redis里的key)里取值(key=token的值,value=用户信息等)判断用户是否已...
JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题。由于同源策略,一般来说位于 server1.example.com 的网页无法与不是 server1.example.com的服务器沟通,而 HTML 的...
后台php设置jsonp
Jsonp和java操作
总算理解jsonp的应用了,以前只知道jsonp但是还是不会再代码中应用,看了这个以后突然恍然大悟,妈妈再也不用担心我学不会jsonp了(/坏笑,以上纯属虚构)
前端使用jquery,datatype采用jsonp,服务端采用C#编写的webService
JSONP 实例
纯servlet实现JSONP 非常简单易懂的一个例子。
ajax jsonp跨域传参,里面有源码,不过写在txt中了,很适合调用别人的接口
基于ajax方式的跨域请求jsonp的前后台代码
交接jsonp,轻松实现跨域方法的调用,很值得学习的东西
Jquery easyui+Jsonp+asp.net+翻页 事件版
jsonp XML解析
对应我的文章json和jsonp格式数据返回
自己封装jsonp.js
该资源为ASP.NET网站开发技术中使用Ajax进行跨域请求时利用JSONP方案解决浏览器同源策略限制的源码,比较简单,仅供参考
使用jsonp跨域获取json数据。Ajax获取JAVA服务器json数据。