Jsonp involved in the same-origin strategy, cross-domain and other issues, here is not elaborate.
The implementation is to create a dynamic script tag to request a background address:
Example:
function (res) { Console.log (res) })
Instance:
function (res) { Console.log (res) })
1 functionjsonp (URL, params, callback) {2 varFuncName = ' Jsonp ' + date.now () + math.random (). toString (). substr (2, 5)3 4 if(typeofparams = = = ' object ') {5 varTemparr = []6 for(varKeyinchparams) {7 varValue =Params[key]8Temparr.push (key + ' = ' +value)9 }Tenparams = Temparr.join (' & ') One } A - varScript = document.createelement (' script ') -script.src = URL + '? ' + params + ' &callback= ' +FuncName the document.body.appendChild (script) - -Window[funcname] =function(data) { - callback (data) + DeleteWindow[funcname] - document.body.removeChild (script) + } A}
JavaScript-Package Jsonp