[Web] What Is JSONP?
JSONP—or JSON with padding—is a sneaky technique that web developers came up with to work around the browser restrictions when requesting data from third-party domains.
It bypasses these restrictions by loading external content using script tags instead of the usual XMLHttpRequest. Adding a script tag to the DOM loads and executes its content directly, and the security restrictions are not applied. The remote request’s content is then normal JSON wrapped in a function call (the P in JSONP). It looks like this:
callbackFn({ a: , b: , c: })
JSONP URLs usually accept a query string parameter so that the caller can specify the name of the callback. The developer then has to define a function in her code that has the same name as the callback in the server response, and when the script tag is added to the document, that function will be called with the JSON data as the first parameter. Libraries like jQuery automate this process by internally creating the global function to handle the JSONP call, and tidying up afterward to avoid polluting the global namespace.
Example:
JSONP data:
eqfeed_callback({
"type": "FeatureCollection",
"metadata": {
"generated": 1408030886000,
"url": "http://earthquake.usgs.gov/earthquakes/...",
"title": "USGS All Earthquakes, Past Day",
"status": 200, "api": "1.0.13", "count": 134
},
"features": [
{
"type": "Feature",
"properties": {
"mag": 0.82,
"title": "M 0.8 - 3km WSW of Idyllwild-Pine Cove, California",
"place": "3km WSW of Idyllwild-Pine Cove, California",
"time": 1408030368460,
...
},
"geometry": {
"type": "Point",
"coordinates": [ -116.7636667, 33.7303333, 17.33 ]
},
"id": "ci15538377"
},
...
]
})
So 'eqfeed_callback' is the callback we will call.
Load JSONP data to the script tag:
function loadJSONP(url) { /* (2) */
var script = document.createElement('script');
script.src = url; var head = document.getElementsByTagName('head')[0];
head.appendChild(script);
}
var quakes = Rx.Observable.create(function(observer){
window.eqfeed_callback = function(response){
var quakes = response.features;
console.log("quakes:", JSON.stringify(quakes, null, 2));
quakes.forEach(function(quake){
observer.onNext(quake)
})
} loadJSONP(QUAKE_URL);
}); quakes.subscribe(function(quake){
var coords = quake.geometry.coordinates;
var size = quake.properties.mag * 1000; L.circle([coords[1], coords[0]], size).addTo(map)
});
We create the callback or let's say the logic to handle the JSONP data, here using RxJS, after subscribe, then we can get the data stream.
[Web] What Is JSONP?的更多相关文章
- 通过扩展让ASP.NET Web API支持JSONP
同源策略(Same Origin Policy)的存在导致了"源"自A的脚本只能操作"同源"页面的DOM,"跨源"操作来源于B的页面将会被拒 ...
- (转)通过扩展让ASP.NET Web API支持JSONP
原文地址:http://www.cnblogs.com/artech/p/3460544.html 同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的D ...
- ASP.NET Web API 配置 JSONP
之前的一篇博文:jsonp跨域+ashx(示例) 1. 安装 Jsonp 程序集: PM> Install-Package WebApiContrib.Formatting.Jsonp PM&g ...
- Web API 实现JSONP或者安装配置Cors跨域
前言 照理来说本节也应该讲Web API原理,目前已经探讨完了比较底层的Web API消息处理管道以及Web Host寄宿管道,接下来应该要触及控制器.Action方法,以及过滤器.模型绑定等等,想想 ...
- 通过扩展让ASP.NET Web API支持JSONP -摘自网络
同源策略(Same Origin Policy)的存在导致了“源”自A的脚本只能操作“同源”页面的DOM,“跨源”操作来源于B的页面将会被拒绝.同源策略以及跨域资源共享在大部分情况下针对的是Ajax请 ...
- web.py实现jsonp
浏览器端请求 $.getJSON("/currenttime?callback=?", function (json){ $("#time").html(jso ...
- ahjesus 让我的MVC web API支持JsonP跨域
无数被跨域请求爆出翔来的人 遇到请求成功却不能进入success 总是提示parsererror 参考一下两篇文章吧 参考文章http://www.asp.net/web-api/overview/s ...
- JavaScript跨域调用、JSONP、CORS与ASP.NET Web API[共8篇]
[第1篇] 同源策略与JSONP 浏览器是访问Internet的工具,也是客户端应用的宿主,它为客户端应用提供一个寄宿和运行的环境.而这里所说的应用,基本是指在浏览器中执行的客户端JavaScript ...
- [CORS:跨域资源共享] 同源策略与JSONP
Web API普遍采用面向资源的REST架构,将浏览器最终执行上下文的JavaScript应用Web API消费者的重要组成部分."同源策略"限制了JavaScript的跨站点调用 ...
随机推荐
- 3月下旬剩余poj题解
poj1700 数学推导+简单dp poj2390 水题不说什么了 poj3260 先对找的钱做完全背包,在对能付的钱做多重背包,注意这道题能付的钱数的上界 poj2516 裸的最小费用最大流了没什么 ...
- php discuz框架接口不能正常访问的问题
本人php小白,无php编程基础,直接上php服务器部署,后果很严重.....所以务必看完请给”顶“给评论,以表示对小白的鼓励和赞赏! 关于discuz框架,独自加班,废寝忘食,然已无力吐槽..... ...
- 排序算法(C#)
1.插入排序 1.1直接插入排序 算法介绍: 直接插入排序(straight insertion sort)的做法是: 每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序. ...
- SSL 通信原理及Tomcat SSL 配置
SSL 通信原理及Tomcat SSL 双向配置 目录1 参考资料 .................................................................. ...
- iOS 之NSJSONReadingOptions说明【转】
首先用代码来说明NSJSONReadingMutableContainers的作用: NSString *str = @"{\"name\":\"kaixuan ...
- NOIP2011 观光公交
3.观光公交 (bus.cpp/c/pas) 风景迷人的小城 Y 市,拥有 n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特 意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 ...
- 专门为公共部门和联邦机构所设计Microsoft Azure
微软正式发布Microsoft Azure for Government,该云平台专门为公共部门和联邦机构所设计. 在2014年三月微软联邦执行官论坛上宣布的Microsoft Azure for G ...
- java中String类小结
构建一个字符串 1.用字符串直接量: String message = new String("Welcome to java"); 2.用字符串直接量: String messa ...
- HDU-4696 Answers 纯YY
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4696 题意:给一个图,每个点的出度为1,每个点的权值为1或者2.给n个询问,问是否能找到一条路径的权值 ...
- hdu5792--World is Exploding
题意:给一个数列,求四个各不相同的数,一个逆序对,一个正序对,求多少组这样的四个数. 题解:辣鸡如我,还是上官方题解了. rg(i)就是i右边比i大的数的个数,rs(i)就是i右边比i小的数的个数. ...