跨域请求解决方案之--JSONP
来源:互联网 发布:marjakurki知乎 编辑:程序博客网 时间:2024/04/27 09:10
JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题。由于同源策略,一般来说位于 server1.example.com 的网页无法与不是 server1.example.com的服务器沟通,而 HTML 的<script> 元素是一个例外。利用 <script> 元素的这个开放策略,网页可以得到从其他来源动态产生的 JSON 资料,而这种使用模式就是所谓的 JSONP。用 JSONP 抓到的资料并不是 JSON,而是任意的JavaScript,用 JavaScript 直译器执行而不是用 JSON 解析器解析。
使用
1. 在客户端调用提供JSONP支持的URL Service,获取JSONP格式数据。
比如客户想访问http://www.yiwuku.com/myService.aspx?jsonp=callbackFunction
假设客户期望返回JSON数据:["customername1","customername2"]
那么真正返回到客户端的Script Tags: callbackFunction([“customername1","customername2"])
可能的调用方式:
1
<script type=
"text/javascript"
src=
"http://www.yiwuku.com/myService.aspx?jsonp=callbackFunction"
></script>
2. 在客户端写callbackFunction函数的实现
1
2
3
4
5
6
7
8
9
10
11
12
<script type=
"text/javascript"
>
function
CustomerLoaded(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>
3. 页面展示
1
<
div
id
=
"divCustomers"
></
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 html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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.yiwuku.com/myService.aspx?jsonp=onCustomerLoaded"
></script>
</body>
</html>
JQuery下:
- $.getJSON
1
2
3
4
5
6
7
8
9
10
11
<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)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://www.yiwuku.com/getdata'
,
success:
function
(){
//dostuff
},
});
如何在服务器端实现对JSONP支持
这仅仅需要把服务的JSON数据转换成想要的script tags的形式就可以了,格式可以自已定义,毕竟这是个非官方的协议。
可参考:Implement JSONP in your Asp.net Application
注:Callback仅仅是JSONP的简单实现,可以根据具体需要实现更复杂的功能,比如可以在客户端动态集成更多的变量数据来完成分页功能。
JSONP支持GET请求,不支持POST请求
0 0
- 跨域请求解决方案之--JSONP
- 跨域请求之JSONP
- 跨域请求之JSONP
- 跨域请求HTTP数据之JSONP
- ajax 跨域请求之jsonp
- 跨域请求之二:jsonp详解
- 基础知识之跨域请求JSONP
- web跨域请求<一>之JSONP
- AJAX跨域请求之JSONP
- Ajax跨域请求解决方案——jsonp
- jsonp跨域请求
- jsonp 跨域请求
- jsonp跨域请求
- jsonp跨域请求
- jsonp跨域请求
- jsonp跨域请求
- JSONP 跨域请求
- jsonp跨域请求
- Redis Cluster集群的搭建与实践
- 简易线程池实现
- UIButton之 setBackgroundImage 和setImage区别 跟UIButton的文字却不显示
- 浅谈垂直居中
- DanmakuFlameMaster无缝续播弹幕的实现
- 跨域请求解决方案之--JSONP
- UIView转换成UIView
- GFOJ problem 470: [HbFS-]Red is good 解题报告
- WPF UserControl弹出子窗口切换界面时消失
- js基本类型 引用类型 简单赋值 对象引用
- SAP HR Schema 详解(一)Payroll schema基础
- JAVA RPC:从上手到爱不释手
- 记录2
- 一些感悟