jsonp的优缺点

转载:http://www.w3cfuns.com/notes/18271/df9ecd8f0ca5e523ae75745a3996c47c.html

JSONP的优缺点
        1.优点
                1.1它不像XMLHttpRequest对象实现的Ajax请求那样受到同源策略的限制,JSONP可以跨越同源策略;
                1.2它的兼容性更好,在更加古老的浏览器中都可以运行,不需要XMLHttpRequest或ActiveX的支持
                1.3在请求完毕后可以通过调用callback的方式回传结果。将回调方法的权限给了调用方。这个就相当于将controller层和view层终于分开了。我提供的jsonp服务只提供纯服务的数据,至于提供服务以 后的页面渲染和后续view操作都由调用者来自己定义就好了。如果有两个页面需要渲染同一份数据,你们只需要有不同的渲染逻辑就可以了,逻辑都可以使用同 一个jsonp服务。
        2.缺点
                2.1它只支持GET请求而不支持POST等其它类型的HTTP请求
                2.2它只支持跨域HTTP请求这种情况,不能解决不同域的两个页面之间如何进行JavaScript调用的问题。
                2.3 jsonp在调用失败的时候不会返回各种HTTP状态码。
                2.4缺点是安全性。万一假如提供jsonp的服务存在页面注入漏洞,即它返回的javascript的内容被人控制的。那么结果是什么?所有调用这个 jsonp的网站都会存在漏洞。于是无法把危险控制在一个域名下…所以在使用jsonp的时候必须要保证使用的jsonp服务必须是安全可信的。

================================================

jquery的$.get $.post $.ajax $.getJSON.

转载:http://www.cnblogs.com/ranzige/p/jquery_get_ajax.html

转载:http://www.jb51.net/article/77470.htm

$.getJSON 的本质是$.get+一个json的参数,get请求才可以跨域。

$.ajax对原声 ajax的一个封装,支持get和post,也可以跨域

================================================

我们通常用的getJSON的用法。

getJSON: function(param) {
            var url = param.url,
                data = param.data,
                success = param.success,
                fail = param.fail,
                callback = param.callback || 'callback',
                timeout = param.timeout || 5000,
                maxCount = param.maxCount || 2;

            var jsonpCallback,
                count = -1,
                delayID = 0,
                s,
                self = this,
                head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;

            var tmp = [];

            if(data) for(var k in data) tmp.push(k + '=' + data[k]);

            if(!/\?/.test(url)){
                url += '?' + tmp.join('&');
            }else{
                url += '&' + tmp.join('&');
            }

            var addCb = function(){
                var time = self.now(),
                    jsonpCallback = 'vjs_' + time + Math.floor(Math.random()*100);

                window[jsonpCallback] = function(response) {
                    destroy();

                    success.call(this, response,{responseTime: self.now()-time, retryCount: count});

                    window[jsonpCallback] = null;
                };

                return jsonpCallback;
            };

            var destroy = function(){
                if(window[jsonpCallback])  window[jsonpCallback] = emptyFn;
                clearTimeout(delayID);

                if (s && s.parentNode) {
                    head.removeChild(s);

                    s.onload = s.onreadystatechange = null;

                    s = undefined;
                }
            };

            var load = function() {
                destroy();
                count++;
                if (count >= maxCount){
                    fail && fail.call(this);
                    return;
                }

                jsonpCallback = addCb();

                var jsonpUrl = url;

                if(/(\=)\?(&|$)/i.test(jsonpUrl)){
                    jsonpUrl = jsonpUrl.replace(/(\=)\?(&|$)/i,'$1' + jsonpCallback + '$2');
                }else{
                    jsonpUrl += '&' + callback + '=' + jsonpCallback;
                }

                if(param.log) param.log.pushLog('lib getJSON===' + jsonpUrl + '===' + jsonpCallback + '====' + count);
                s = document.createElement('script');
                s.setAttribute('type', 'text/javascript');
                s.setAttribute('src', jsonpUrl);

                head.insertBefore(s, head.firstChild);

                delayID = setTimeout(load, timeout);
            };

            load();

            return {
                destroy : destroy
            }
        }

  

 

posted @ 2016-10-11 11:01  飘然离去  阅读(5876)  评论(0编辑  收藏  举报