JSONP

JSONP是利用浏览器对script的资源引用没有同源限制,通过动态插入一个script标签,当资源加载到页面后会立即执行的原理实现跨域的。JSONP是一种非正式传输协议,该协议的一个要点就是允许用户传递一个callback或者开始就定义一个回调方法,参数给服务端,然后服务端返回数据时会将这个callback参数作为函数名来包裹住JSON数据,这样客户端就可以随意定制自己的函数来自动处理返回数据了。
之前有看到一个有形象的解释:Jsonp:json+padding(内填充),把json当做内填充东西,填充到盒子中。

//JSONP代码,转自switer
function () {
    var _cbs = {}
    var _ns = '_jsonp'
    var _pn = 'callback'
    var _id = 0
    var _t = 20000

    function noop () {}

    function jsonp (url, cb, options) {

        options = options || {}
        cb = cb || noop

        var s = document.createElement('script')
        var cid = _ns + _id ++
        var ended

        window[cid] = function (data) {
            onsuccess(data)
            window[cid] = onsuccess = onerror = noop
        }
        function onsuccess (data) {
            document.head.removeChild(s)
            cb(null, data)
        }
        function onerror (e) {
            document.head.removeChild(s)
            cb(e || 'error')
            window[cid] = onsuccess = onerror = noop
        }
        s.onerror = s.onabort = function (e) {
            onerror(e ? e.type : 'error')
        }
        url = url.replace(/[\?\&]$/, '')
        url = url + (~url.indexOf('?') ? '&' : '?') + _pn + '=' + cid
        s.src = url
        document.head.appendChild(s)
        setTimeout(function () {
            onerror('timeout')
        }, options.timeout || _t)
    }
    jsonp.timeout = function (t) {
        _t = t
    }
    jsonp.ns = function (n) {
        _ns = n
    }
    jsonp.pn = function (n) {
        _pn = n
    }

    /**
     *  Global namespace is "Routed"
     */
    if (typeof module !== 'undefined' && module.exports) module.exports = jsonp
    else if (typeof exports !== 'undefined') exports.jsonp = jsonp
    else window.jsonp = jsonp
}();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值