[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: 1, b: 2, c: 3})

 

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.

posted @   Zhentiw  阅读(280)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-08-14 [CSS] Pseduo
2014-08-14 [Backbone] First Application!!!!
点击右上角即可分享
微信分享提示