. 1 <Script> 2 / * . 3 JSONP ({ . 4 URL: 'HTTP: // localhost: 3000 /', . 5 Data: '', . 6 Success: function (Data) { . 7 the console.log (Data); . 8 } . 9 }) 10 * / . 11 12 is ( function (W) { 13 is / * * 14 * JSONP achieved 15 * @param {Object} Option 16 * / . 17 function JSONP (Option) { 18 is // 1. mount global function . 19 w.getData = option.success; 20 is // 2. Processing url link 21 is option.url = option.url + '= the getData the callback?' ; 22 is // 3. Create the script tag insertion body 23 is var scriptEle = document.createElement ( 'Script' ); 24 scriptEle.src = option.url; 25 document.body.appendChild (scriptEle); 26 is } 27 w.jsonp = JSONP; 28 }) (window); 29 </ Script> 30 <Script> 31 is JSONP ({ 32 url: 'http://localhost:3000/', 33 success: function (data) { 34 console.log(data); 35 alert('111'); 36 } 37 }); 38 39 jsonp({ 40 url: 'http://localhost:3000/', 41 success: function (data) { 42 console.log(data); 43 alert('222'); 44 } 45 }); 46 47 jsonp({ 48 url: 'http://localhost:3000/', 49 success: function (data) { 50 console.log(data); 51 alert('333'); 52 } 53 }); 54 </script>
The results cover multiple calls occur
. 1 <Script> 2 ( function (W) { . 3 / * * . 4 * JSONP achieved . 5 * @param {Object} Option . 6 * / . 7 function JSONP (Option) { . 8 // of 0. The generating function different names . 9 var .. callBackName = 'itLike' + Math.random () toString () substr (2) + Math.random () toString () substr (2.. ); 10 // the console.log (callBackName); . 11 // . 1 the function global mount 12 is W [callBackName] = option.success; 13 is // 2. url link process 14 option.url = option.url + '?callback=' + callBackName; 15 // 3. 创建script标签插入body 16 var scriptEle = document.createElement('script'); 17 scriptEle.src = option.url; 18 document.body.appendChild(scriptEle); 19 } 20 w.jsonp = jsonp; 21 })(window); 22 </script> 23 <script> 24 jsonp({ 25 url: 'http://localhost:3000/', 26 success: function (data) { 27 console.log(data); 28 alert('111'); 29 } 30 }); 31 32 jsonp({ 33 url: 'http://localhost:3000/', 34 success: function (data) { 35 console.log(data); 36 alert('222'); 37 } 38 }); 39 40 jsonp({ 41 url: 'http://localhost:3000/', 42 success: function (data) { 43 console.log(data); 44 alert('333'); 45 } 46 }); 47 </script>
. 1 <Script> 2 ( function (W) { . 3 / * * . 4 * JSONP achieved . 5 * @param {Object} Option . 6 * / . 7 function JSONP (Option) { . 8 // of 0. The generating function different names . 9 var .. callBackName = 'itLike' + Math.random () toString () substr (2) + Math.random () toString () substr (2.. ); 10 // the console.log (callBackName); . 11 // . 1 the function global mount 12 is W [callBackName] = function (Data) { 13 is option.success (Data); 14 // delete the script tag 15 document.body.removeChild (scriptEle); 16 }; . 17 // 2. Processing url link 18 is option.url = option.url + '? the callback =' + callBackName ; 19 // Create the script tag insertion body 20 is var scriptEle = document.createElement ( 'script' ); 21 is scriptEle.src = option.url; 22 is document.body.appendChild (scriptEle); 23 is } 24 w.jsonp = JSONP; 25 })(window); 26 </script> 27 <script> 28 jsonp({ 29 url: 'http://localhost:3000/', 30 success: function (data) { 31 console.log(data); 32 alert('111'); 33 } 34 }); 35 36 jsonp({ 37 url: 'http://localhost:3000/', 38 success: function (data) { 39 console.log(data); 40 alert('222'); 41 } 42 }); 43 44 jsonp({ 45 url: 'http://localhost:3000/', 46 success: function (data) { 47 console.log(data); 48 alert('333'); 49 } 50 }); 51 </script>