jsonp跨域

 1 (function(window, document, undefined) {
 2   'use strict';
 3   var jsonp = function(url, data, callback) {
 4     var fnSuffix = Math.random().toString().replace('.', '');
 5     var cbFuncName = 'my_json_' + fnSuffix;
 6     window[cbFuncName] = callback;
 7     var querystring = url.indexOf('?') == -1 ? '?' : '&';
 8     for (var key in data) {
 9       querystring += key + '=' + data[key] + '&';
10     }
11     querystring += 'callback=' + cbFuncName;
12     var scriptElement = document.createElement('script');
13     scriptElement.src = url + querystring;
14     document.body.appendChild(scriptElement);
15   };
16   window.$jsonp = jsonp;
17 })(window, document);

调用

 1   <div id="result"></div>
 2   <script>
 3     (function() {
 4       $jsonp(
 5         'http://api.xxx..com/xx/xx', {
 6           page: 10,
 7           count:15
 8         },
 9         function(data) {
10           document.getElementById('result').innerHTML = JSON.stringify(data);
11         });
12     })();
13   </script>
原文地址:https://www.cnblogs.com/Doduo/p/6528287.html