[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的跨站点调用 ...
随机推荐
- BZOJ1758: [Wc2010]重建计划
题解: 这题我居然做了一星期?... 平均值的极值其实也可以算是一种分数规划,只不过分母上b[i]=1 然后我们就可以二分这个值.类似与 HNOI最小圈 如果没有 链的长度的限制的话,我们直接两遍df ...
- PHP搭建OAuth2.0
这几天一直在搞OAuth2.0的东西,写SDK啥的,为了更加深入的了解服务端的OAuth验证机制,就自己动手搭了个php下OAuth的环境,并且将它移植到了自己比较熟的tp框架里. 废话不多说,开动. ...
- 【转】[MTK软件原创] [SELinux] 如何设置确认selinux模式
原文网址:http://bbs.16rd.com/thread-54766-1-1.html [Description] linux SELinux 分成Enforce 以及 Permissive 两 ...
- jetty属性
jetty 版本信息 Jetty7 - 此插件更名为jetty-maven-plugin,以便更符合maven2的协定.为了在Web应用做快速应用开发做准备,详见多Web应用源目录. 为 ...
- Python 连接mysql
下面我们使用MySQLdb 实现连接mysql 数据库并进行操作. #!/usr/bin/env python # -*-coding:UTF-8-*- import MySQLdb def conn ...
- .Net高级技术
本次课程中讲的有的东西都是根据初学者的认知规律进行了调整,并不是严谨的,比如很多地方在多AppDomain条件下很多说法就不对了,但是说严谨了大家就晕了,因此继续不严谨的讲吧. 很多面试题都在这阶段的 ...
- Mac 中查看端口占用进程并杀死
sudo lsof -i :9000 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 61342 a 313u IPv6 0x11111 ...
- JDBCTemplate.java
package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...
- U盘安装CentOS 6.5注意事项
这两天新到一批机器,需要安装系统.买的机器没有光驱,只能使用U盘或者网络安装.为了方便,我使用UltraISO对U盘进行刻录制作了启动盘.一切都是那么顺利,安装完成了,没出现什么问题(毕竟 ...
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...